jsha/README

a blog by Jacob Hoffman-Andrews

HOWTO Turn a shapefile into a KML file under Ubuntu

without comments

Shapefiles and KML are two common geographic formats.  Shapefiles tend to be used by geographic databases like ESRI and PostGIS, while KML originated with Google Earth, back when it was called Keyhole.  KML files are more common for data that is intended primarily to display to users.  They are supported both in Google Earth and in Google Maps, through importing to a My Map.

But for all that both formats are really popular, it’s not easy to go back and forth between the two.  Further, it’s really hard to simplify a KML file that has highly detailed polygons.  This is important because, while Google Earth may be able to handle a large number of points, Google Maps has to execute in the browser and so has tight limits on how many points can be in a polygon.  This can result in a group of polygons being split by Maps across multiple pages so they can be rendered in a reasonable amount of time.

I had some shapefile data I wanted to simplify and display in a My Map. My solution was to bite the bullet and install PostgreSQL and PostGIS.  Both are free software and are available as packages on Ubuntu Intrepid Ibex. Similar directions should apply for other Linux distributions.

First, install PostgreSQL and the PostGIS geographic layer on top of it:


$ sudo apt-get install postgresql-8.3-postgis postgis gdal-bin

Become the postgres user to create the procedural language, initialize the geographic functions, and fill the spatial_ref_sys table:


$ sudo su postgres
postgres$ createlang plpgsql
postgres$ psql -d postgres -f /usr/share/postgresql-8.3-postgis/lwpostgis.sql
postgres$ psql -d postgres -f /usr/share/postgresql-8.3-postgis/spatial_ref_sys.sql
postgres$ createuser username
Shall the new role be a superuser? (y/n) y
CREATE ROLE
postgres$ exit

Import the shapefile into your database. The -d parameter drops the table before creating it, useful if you may be reimporting the data multiple times. If you have a large file you may want to add the -D parameter, which uses the more efficient dump format for faster ingestion. The -s parameter specifies the SRID of your input data, and you should be able to figure it out from the .prj file that came with the shapefile.


$ shp2pgsql -d -s 2877 election_precincts election_precincts_table postgres | psql -d postgres

And dump output into KML.  Note that you can put any criteria you want into the -sql param, so you could restrict the set of features you output.


$ ogr2ogr -f "KML" election_precincts.kml PG:"dbname=postgres" -dsco NameField=precinct -sql "select precinct, transform(simplify(the_geom, 100), 4326) from election_precincts_table"

Note: KML explicitly supports only one SRID: 4326. That refers to WGS84. So in the ogr2ogr command above, you need the transform(…, 4326) call to produce valid lat/longs for KML. Evidently Google Maps has another SRID, 900913, but I haven’t played with that one at all. If you’re using KML as your input to Google Maps, 4326 should be fine.

You can increase or decrease the parameter to simplify(). Higher numbers mean more simplification, lower numbers mean less.

The NameField param is a KML-specific parameter to ogr2ogr which specifies which field from the query should be used as the name for a given KML feature.

Here are some other pages I found useful when working this all out:

PostGIS Wiki : Postgis On Ubuntu
PerryGeo » Converting Shapefiles (and more) to KML
Projected coordinates listed by name
PostGIS Manual
PostGIS Simplify
PostGIS ver. 1.3.1 Quick Guide – Cheatsheet
#2271 (Add built in reprojection support to KML driver) – GDAL – Trac
[postgis] SRID for LAT/LONG
[postgis-users] addgeometrycolumn() does not exist
[FWTools] ogr2ogr shp –> kml conversion failing on some complicated polygons
Generalize Vectors – GEO UTILITIES

Written by jsha

October 29th, 2008 at 11:50 am

Posted in HOWTO

Leave a Reply