file conversion and geodatabase

Embed Size (px)

Citation preview

  • 8/19/2019 file conversion and geodatabase

    1/12

    30

    Exercise 3: File conversion and

    geodatabase 

  • 8/19/2019 file conversion and geodatabase

    2/12

    31

    6  Downloading OpenStreetMap data and converting to geodatabase,

    vector files and raster files

    6.1  Introduction

    Often the files that we use in a GIS need to be converted to other formats. We can think of the

    following cases:

      One raster format to another raster format: if we use a GIS for pre- or postprocessing of data

    for a tool or a model, the data needs to be read or produced in the format supported by the

    tool or the model. Thanks to the Geospatial Data Abstraction Library (GDAL) many

    conversions are supported in commercial and open source GIS applications

      One vector format to another vector format: similar to raster, but here often the OGR simple

    features library is used to translate from one to another format.

      From raster to vector: this is useful when analysis is done using vector geoprocessing

    algorithms. This is also useful for presenting data to decision makers who often prefer data in

    vector format.  From vector to raster: in order to perform map algebra or other raster analysis, vector data

    can be converted to raster data.

      Conversion to and from geodatabases: instead of using files, GIS data can also be organized

    in geodatabases.

    In this exercise we will download OpenStreetMap data for your area of interest. OpenStreetMap

    (OSM) is a collaborative project to create a free editable map of the world

    (http://openstreetmap.org). Next we will import the data into a geodatabase. After doing some

    queries, the resulting data will be saved in shapefiles. Some of the shapefiles will be converted to

    KML to visualize in Google Earth and other shapefiles will be converted to raster in order to apply

    map algebra.

    6.2  Installing the OpenLayers Plugin

    First we have to install the OpenLayers plugin. This plugin enables the visualization of web data, such

    as OpenStreetMap, Bing and Google Maps. Follow these steps to install the plugin:

    1. 

    In the main menu go to Plugins   Manage and install plugins….

    2.  At Search type: OpenLayers.

    3.  Install the OpenLayers plugin

    6.3  Installing the GeoCoding plugin

    The GeoCoding plugin uses the Nominatim and Google web sevices to enable searching for an

    address and get the coordinates (geocoding) or to click on the map canvas and get the address

    (reverse geodcoding). This plugin will help us to easily find our area of interest. Now install the

    GeoCoding plugin in a similar way as the OpenLayers plugin.

    6.4 

    Search for your area of interest

    Use the GeoCode plugin to search for your area of interest:

    1. 

    In the main menu choose: Plugins  GeoCode  GeoCode 

    http://openstreetmap.org/http://openstreetmap.org/http://openstreetmap.org/http://openstreetmap.org/

  • 8/19/2019 file conversion and geodatabase

    3/12

    32

    2.  Type for example Kampala in the address dialogue.

    3.  If more than one place is found, you can use the dropdown menu to choose the right one:

    4.  A popup screen shows up where you have to indicate the CRS of the GeoCode layer (your

    search will result in a layer added to the map canvas). You can choose EPSG:4326

     

    6.5 

     Add OpenStreetMap to the map canvas

    We can have a free map of the area of interest by loading OpenStreetMap to the map canvas. Do the

    following:

    1. 

    From the main menu choose Web  OpenLayers plugin  OpenStreetMap  

    OpenStreetMap.

  • 8/19/2019 file conversion and geodatabase

    4/12

    33

    2. 

    Zoom in to your area of interest. You can move the GeoCode layer to the top, so

    OpenStreetMap is in the background.

    Now look at the map. What kind of features can you see? Is the map vector or raster?

    6.6  Downloading OpenStreetMap vector data

    The OpenStreetMap data loaded in the map canvas comes from a web map service. In fact the

    original data is vector, but it has been rendered to a raster image to be presented in a fast a nice

    way. Often, however, we do not only want to visualize the topographical map of the area of interest,

    but also use the data for analysis. For this purpose we can download the OpenStreetMap data of our

    area of interest, using the following steps:

    1. 

    Zoom in to your area of interest (e.g. center of Kampala).

    2. 

    In the main menu go to Vector   OpenStreetMap   Download Data… 

  • 8/19/2019 file conversion and geodatabase

    5/12

    34

    3.  In the dialogue choose From map canvas for the extent and browse to select an output file

    name (e.g. Kampala.osm). In case internet does not work, this file has been provided so

    you can continue offline with the exercise.

    4.  Click okay and wait until the data has been downloaded. If the popup with the message

    Download has been successful appears, click OK  and Close to go back to return to the map

    canvas. Note that sometimes with a poor internet connection the download can be

    incomplete, but no error message is displayed. This results in an empty attribute table. If this

    occurs, try to download the file again or use the file from the Open CourseWare website. 

    5.  The file you have downloaded is a .osm file. This is an XML format that is supported by OGR.

    Therefore, you can open it easily in QGIS. Click the Add vector layer button and browse

    to the .osm file. Click Open.

    6. 

    A popup window shows up and asks you to select one or more layers from the file. Click

    Select All  and OK .

  • 8/19/2019 file conversion and geodatabase

    6/12

    35

    6.7  Querying vector data

    Let’s assume that we are interested in the primary roads. We can make a selection from the vector

    attribute table and export the selection to a new vector file.

    1.  Click right the lines layer (lines any) and choose Open Attribute Table… 

    2.  Inspect the attribute table. You can see that it contains for example street names and road

    types. Now we’re going to select the primary roads from the attribute table. Click (Select

    features using an expression) from the toolbar on top of the attribute table.

    3.  In the dialogue that opens, we can build the expression for the selection of primary roads.

    a.  First click on the + sign before Fields and Values

    b. 

    Double click on highway. You can see that “highway” has been inserted under

    expression.

    c. 

    On the lower right, under values, you can click all unique load all unique values.

    d.  Click on the = button

    e. 

    Double click on ‘primary’. Now the expression should look like this:

    f.  Click Select  and Close.

  • 8/19/2019 file conversion and geodatabase

    7/12

    36

    4.  In the title of the attribute table window you can see the number of selected features out of

    the total. On the lower left you can click on Show selected features.

    6.8  Exporting vector data

    1. 

    In the map canvas these selected features are highlighted in yellow. We can now export the

    selected lines to a new vector file in any OGR format (e.g. Google KML or ESRI Shapefile).

    Click again right on the lines layer (lines any) and choose Save as… 

    2. 

    In the dialogue choose ESRI Shapefile as the output format. Save the file as

    PrimaryRoads.shp. Here you can also change the CRS of the output file. Click on the

    select CRS button, choose EPSG:21096 and click OK. This is the projection used in Uganda,

    north of the equator (UTM Zone 36N/Arc 1960).

  • 8/19/2019 file conversion and geodatabase

    8/12

    37

    3. 

    Click Save only selected features (if we don’t check the box we can convert the entire lines

    layer to another OGR format), check the box to Add saved file to map and click OK .

    4.  Repeat the procedure to select from the points layer (points any) the manmade towers

    (“man-made = ‘tower’) and save the results to Towers.shp 

  • 8/19/2019 file conversion and geodatabase

    9/12

    38

    5.  Repeat the procedure to select from the polygon layer (multipolygons any) the

    commercial buildings (“buildings” = ‘commercial’) and save the results to

    Commercial.shp 

    6.9 

    Vector to raster

    If we want to calculate for example Euclidean distance from commercial buildings, which is a rasteroperation, we need to convert our shapefile with commercial buildings to raster format.

    1.  First we need to add a column to the attribute table of Commercial.shp, so we can add a

    value of 1 for the existing buildings. In the attribute table click on to toggle to editing

    mode.

    2.  Next, click on to add a new column. In the Add column dialogue type Value at the Name

    of the column. Choose Whole number  and give 1 for the width and click OK .

    3.  Now we can type the expression directly at the top of the attribute table: Value = 1. Click

    Update All . Now the Value column will be filled with ones.

    4.  Click to toggle back and save the edits.

    5. 

    From the main menu choose Raster   Conversion   Rasterize (Vector to Raster)…

     

  • 8/19/2019 file conversion and geodatabase

    10/12

    39

    6.  Fill in the dialogue as below and click OK , OK , OK  and Close. This results in a Boolean raster of

    3000 x 3000 pixels with value 1 for the commercial areas.

    Use the Identify tool to check the values of commercial and non-commercial pixels.

    6.10  Saving the database

    Instead of using separate .tif and .shp files, we can also save the layers in a geospatial database.

    Here we will create SpatiaLite database, a file based database.

    1.  Go to the Browser  and click right on SpatiaLite and select Create database 

    2.  Browse to the folder where you want to save it and give it a name, e.g. Kampala.sqlite.

    A popup window informs us that the database has been created.

  • 8/19/2019 file conversion and geodatabase

    11/12

    40

    3.  Now we have to fill the database. In the main menu choose: Database   DB Manager    DB

     Manager .

    4.  A new window appears where we can control the databases in QGIS. Click on the + sign

    before SpatiaLite to see the databases. Then select Kampala.sqlite.

    5.  The database is empty and we need to import the layers. Click on the Import layer/file

    button .

    6.  Choose the layer to input. Start with the Tower points. Click Update options to pre-fill some

    of the form option. Specify the source SRID and target SRID as 21096. Enable the check boxto create a spatial index. Click OK  to perform the import.

    7. 

    Click OK  when the popup shows up that the import was successful.

    8. 

    Click the refresh button to see the added layer in the database.

    21096 21096

  • 8/19/2019 file conversion and geodatabase

    12/12

    41

    9.  Repeat the same steps for PrimaryRoads and Commercial. Note that we cannot import

    raster data. Finally the DB Manager should have the points, lines and polygons.

    Close the DB manager.

    6.11  Adding layers from the database to the map canvas

    1. 

    From the Layers list remove all vector layers, so we only have OpenStreetMap and the

    CommercialRast. You can do this by click right on the layer name and choosing Remove.

    2. 

    Click the Add SpatiaLite Layer button .

    3. 

    In the dialogue that shows up make sure the right database is chosen, e.g.

    Kampala.sqlite, and click Connect.

    Select all the layers and click Add . The layers will be loaded into the map canvas.