| < Day Day Up > |
Chapter 7. Converting Map Data
Chapter 2 outlined the various tasks of digital mapping; one of those
The examples in this chapter use the GDAL/OGR utilities that are introduced in Chapter 6. They are part of the FWTools package. |
| < Day Day Up > |
| < Day Day Up > |
7.1. Converting Map Data
Different data formats have sprung up because of the need for various capabilities and the
Data conversion can be necessary for a number of reasons:
|
| < Day Day Up > |
| < Day Day Up > |
7.2. Converting Vector DataQuite often data isn't in a format that makes it readily available for both mapping software and people to read. Many data formats store the geographic data in a binary format. This format is normally readable only by computers and is designed for software to use. Some spatial data formats are in a simple text format, which is easier to explore. 7.2.1. Geography Markup Language (GML)One example of a text-based format is Geography Markup Language (GML). It uses a text syntax to encode coordinates into a text file. GML can then be read or manually edited without needing a special piece of software or at least nothing more than a common text editor. Creating GML from scratch isn't very pleasant. Fortunately, another OGR utility exists that can convert OGR-supported data formats into and out of other formats, including GML. GML has three different versions: GML1, GML2, and GML3. There are many sub-versions as well. The differences between versions can be a problem because certain features may not be directly transferable to another. The tools introduced in this chapter use ogr2ogr , which outputs to GML2 format.
GML was designed to be suitable for data interoperability, allowing the exchange of spatial data using a common format. This has opened up the possibilities for various web services that
GML's downside is its
Extensible Markup Language (XML) is a popular standard for general data exchange,
7.2.2. Converting a
|
|
When editing the airports.gml file, you can see there are hierarchical sections in the file. Each airport feature has a section starting with <gml:featureMember> and ending with </gml:featureMember> . The last attribute listed in each section is <ogr:geometryProperty> . This is the section containing all the geometry information. To edit an airport file, simply change the coordinates that are entered between the <gml:coordinates> tags. For example:
<gml:coordinates>444049,5277360</gml:coordinates>
can be changed to:
<gml:coordinates>450000,5260000</gml:coordinates>
ogrinfo can be used again to view information about the GML file, or ogr2ogr can be used to convert the data back into the shapefile format someone else may be expecting. This is shown in the following example:
> ogr2ogr -f "ESRI Shapefile" airport_gml.shp airports.gml airports
This takes the GML file and converts it back to shapefile format. Now ogrinfo can be run on it to compare the results with the earlier tests.
You will find that sometimes you get a set of data for a project but are interested in only a small portion of it. For example, if I'm making a map of my local municipality, I might want only the location of the
It is possible to use ogr2ogr as a feature extraction tool. For example, if you want to make a map of the Bigfork airport, you can use ogr2ogr and the -where option to create a new shapefile that only includes the Bigfork Municipal Airport, as shown in the following example:
> ogr2ogr -f "ESRI Shapefile" bigfork data/airports.shp -where "name='Bigfork Municipal Airport'"
The first parameter
> ogrinfo bigfork airports INFO: Open of 'bigfork' using driver 'ESRI Shapefile' successful. Layer name: airports Geometry: Point Feature Count: 1 Extent: (451306.000000, 5291930.000000) - (451306.000000, 5291930.000000) Layer SRS WKT: (unknown) NAME: String (64.0) LAT: Real (12.4) LON: Real (12.4) ELEVATION: Real (12.4) QUADNAME: String (32.0) OGRFeature(airports):0 NAME (String) = Bigfork Municipal Airport LAT (Real) = 47.7789 LON (Real) = -93.6500 ELEVATION (Real) = 1343.0000 QUADNAME (String) = Effie POINT (451306 5291930)
The power of ogr2ogr is its ability to handle several vector data formats. To get a list of possible formats, run ogr2ogr without any parameters, as shown in Example 7-3. You will see the help information displayed as well as the -f format name options.
-f format_name: output file format name, possible values are:
-f "ESRI Shapefile"
-f "TIGER"
-f "S57"
-f "MapInfo File"
-f "DGN"
-f "Memory"
-f "GML"
-f "PostgreSQL"
Creating other formats is as easy as changing the
-f
option to the needed format, then entering in an appropriate output dataset name. The
-where
clause can
|
Converting to other
> ogr2ogr -f "GML" bigfork_airport.gml data/airports.shp -where "name='Bigfork Municipal Airport'"
This example shows how to convert to DGN format:
> ogr2ogr -f "DGN" -select " " bigfork_airport.dgn data/airports.shp -where "name='Bigfork Municipal Airport'"
DGN format can't support attribute fields. When converting to DGN, you'll see an error message:
ERROR 6: CreateField( ) not supported by this layer.
The file is created, but it doesn't include any attributes. One workaround to this problem is to use the -select option. With other formats, the -select option allows you to specify what attributes you want to convert into the destination layer. Because ogr2ogr can't convert any attributes to the DGN file, you select no fields by providing an empty value, such as two double quotes as shown in the DGN example.
ogr2ogr can also convert data to database formats, such as PostgreSQL (using the PostGIS spatial extension if it is available). The syntax for the command is more complicated than simple file-based formats because there are certain parameters that must be used to connect to the destination database. These options aren't discussed in detail here but are covered in Chapter 13.
This example shows how to convert from a shapefile to a PostgreSQL database:
>
ogr2ogr -f "PostgreSQL" "PG:dbname=myairports host=myhost.com user=pgusername"
data/airports.shp -where "name='Bigfork Municipal Airport'"
The command in this example connects to the myairports database on a server called myhost.com using the PostgreSQL database user pgusername . It then creates a table called airports . This is the same name as the input shapefile, which is the default. Querying the database using the PostgreSQL query tool psql shows that the conversion was successful, as in Example 7-4.
psql> select * from airports; -[ RECORD 1 ]+-------------------------------------------------- ogc_fid 1 wkb_geometry SRID=-1;POINT(451306 5291930) name Bigfork Municipal Airport lat 47.7789 lon -93.6500 elevation 1343.0000 quadname Effie
|
More detailed PostgreSQL usage is covered in Chapter 13, where an example problem shows how to load data into a spatial database and also
| < Day Day Up > |