Using ogr to convert gis vector formats
July 18, 2008
OGR is a part of GDAL and is very useful for converting between geospatial vector formats. What does that mean? When storing Vector GIS data there are a dizzying number of formats it can be stored in, some of the more popular of late, or at least well known are KML and SHP. A friend of a friend was looking to convert some SHP (Shapefiles) into KML so that he could make a Google Maps mashup and I helped him out. Here is a workflow for how I went about performing the conversion. OGR can be installed on debian/ubuntu machines by installing the package gdal-bin:
sudo apt-get install gdal-bin
Once you have GDAL/OGR installed you get a slew of command line utilities, I’ll try to cover some others in later tutorials, but for now we’re interested in ogr2ogr. ogr2ogr converts between the vector formats that OGR understands.
Example using a shapefile at the city of chicago website: Download the data
Now we have to do the hard part (not really that hard, but important), look for the projection information in the metadata. I looked in the tifs.shp.xml file and found that the projections is:
Google uses WGS84 spatial reference system. Now we have to lookup EPSG codes that OGR understands for these projections. A good spot to do this is spatialreference.org. EPSG codes provide a short form of expressing projection and spatial reference information. Once we have that all sorted out we’re ready to run ogr2ogr:
ogr2ogr -f "KML" -s_srs "EPSG:102671" -t_srs "EPSG:4326" tifs.kml tifs.shp
The -f “KML” specifies that we want the output in KML. -s_srs is the source (tifs.shp) spatial reference system and -t_srs is the target spatial reference system found at spatialreference.org, then we specify the output file tifs.kml and the input file tifs.shp. That is it!
Lets shrink it down into a kmz (compressed kml) so that it takes up less disk space.
zip tifs.kmz tifs.kml