2

I am currently working with a shapefile from the United States Drought Monitor that countains all drought data from all of the states in the US. I am converting it to geojson with ogr2ogr and would like to filter the geojson output so that it only contains data within the bounds of California. How can I do this?

I am wondering if there are optional arguments I can pass in to something like ogr2ogr -f GeoJSON drought.json USDM_20140617.shp

Or would I only be able to limit the results by doing a join between shapefiles (for example, a join with a shapefile that contained data for California's boundaries)?

Shapefiles are here

Thanks!

1

There are at least two alternatives. The first one is to use the -clipsrc parameter.

ogr2ogr -f "ESRI Shapefile" test.shp world_boundaries_m.shp -clipsrc "POLYGON (( 1308807.8548639829 2728968.1094570993, 823671.391414903 169260.41200850534, 3897264.203215087 209607.73328746122, 2782801.7002406875 2976763.0370215555, 1308807.8548639829 2728968.1094570993 ))"

However, this option clips the geometries with the polygon and you may prefer to select features as they are. In this case the -sql parameter with SQLite dialect is extremely powerful.

ogr2ogr -f "ESRI Shapefile" test2.shp world_boundaries_m.shp -dialect sqlite -sql "select * from world_boundaries_m where ST_Intersects (geometry,geomfromtext('POLYGON (( 1308807.8548639829 2728968.1094570993, 823671.391414903 169260.41200850534, 3897264.203215087 209607.73328746122, 2782801.7002406875 2976763.0370215555, 1308807.8548639829 2728968.1094570993 ))'))"

Depending on your data you may need to adjust the polygon and add SRID after the WKT polygon but the query above worked for me with my test data.

  • Thanks for the answer. Is there a way of doing SQL-style joins between two GeoJSON files (ex. a left or inner join) outputting a new .json file as the result of the join? – user32703 Jun 22 '14 at 21:09
  • I would have a try with GDAL VRT file gdal.org/drv_vrt.html. Create a VRT that publishes your two files as one datasource with two layers and query that with ogr2ogr. – user30184 Jun 23 '14 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.