Skip to content

OGR to reproject, modify Shapefiles

nvkelso edited this page May 30, 2012 · 31 revisions

Install

GDAL includes OGR.

Ubuntu 11.10 - Source

Check to see if you have binutils: dpkg -s binutils

If you don't have binutils, sudo apt-get install binutils

sudo apt-get install gdal-bin

sudo apt-get install libproj-dev

Mac OS X

Install the KyngChaos GDAL frameworks for your version of Mac OS X.

Usage

VPRO reprojection:

to geographic

ogr2ogr -s_srs "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +units=m +no_defs" -t_srs EPSG:4326 output_shapefile.shp input_shapefile.shp

to mercator:

OGR2OGR -f "ESRI Shapefile" -s_srs "+proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs" -t_srs "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over" /Users/nvkelso/github/golden-ratio/scripts/900913/flickr_bbox_merc.shp /Users/nvkelso/github/golden-ratio/scripts/flickr_bbox_join8_redux_poly_area1.shp

ogr2ogr -s_srs "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +units=m +no_defs" -t_srs "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs" output_shapefile.shp input_shapefile.shp

Rename SHP columns:

ogrinfo -so z7-labels.shp z7-labels

ogr2ogr -select 'name, admin1 cod as admin1_cod, point size as point_size , type_rank, name, geonameid, font file as font_file, zoom as zoom_dymo, country co as country_co, font size as font_size, capital, asciiname, type, population’

Sort the OGR columns:

Important for drawing features in the right order (especially town labels, also important for how roads stack).

ogr2ogr -sql "SELECT FEATURECLA, NAME, AGGLOMERA2, BILINGUAL, POP_MAX, POPRANK, LABELRANK, SCALERANK, ADM0_A3, GEONAMEID FROM pop12_ru_merc3 ORDER BY POP_MAX DESC" shp/pop12_ru_merc3{-sorted,}.shp

ogr2ogr -sql "SELECT * FROM dma_cities_a_b_c_d_join ORDER BY POP_MAX DESC" dma_cities_a_b_c_d_join{_sorted,}.shp

ogr2ogr -sql "SELECT * FROM airports_merc ORDER BY natlScale DESC" airports_merc{_sorted,}.shp

Stats on your values

Use -sql flag for extra power. Docs

Shows me I have a couple hundred country codes and what they are:

ogrinfo -sql "select COUNT(DISTINCT ADM0_A3) from ne_10m_populated_places_simple" ne_10m_populated_places_simple.shp ne_10m_populated_places_simple

ogrinfo -sql "select DISTINCT ADM0_A3 from ne_10m_populated_places_simple" ne_10m_populated_places_simple.shp ne_10m_populated_places_simple

Shows me there are just a handful of scale ranks:

ogrinfo -sql "select COUNT(DISTINCT SCALERANK) from ne_10m_populated_places_simple" ne_10m_populated_places_simple.shp ne_10m_populated_places_simple

Describes the actual values in that column:

ogrinfo -sql "select DISTINCT SCALERANK from ne_10m_populated_places_simple" ne_10m_populated_places_simple.shp ne_10m_populated_places_simple

Find out the min, max, avg values in a number column:

ogrinfo -sql "select MIN(SCALERANK) from ne_10m_populated_places_simple" ne_10m_populated_places_simple.shp ne_10m_populated_places_simple

ogrinfo -sql "select MAX(SCALERANK) from ne_10m_populated_places_simple" ne_10m_populated_places_simple.shp ne_10m_populated_places_simple

ogrinfo -sql "select AVG(SCALERANK) from ne_10m_populated_places_simple" ne_10m_populated_places_simple.shp ne_10m_populated_places_simple

Exclude high or low extreme values:

SELECT * from polylayer WHERE prop_value > 220000.0 ORDER BY prop_value DESC

OGR in Python

Has got to be the worst documented API ever.

ChrisG at USU.edu has a good tutorial

Want the power of OGR in Python? Try this other tools: Fiona from @sgillies wrapping OGR and the awesome shapely.

Clone this wiki locally