1

I would like to extract a number of features from a ShapeFile which match with specific latitude/longitude values. I am interested if/how this can be done with QGIS and ogr2ogr. The output should be a ShapeFile as well; for QGIS a layer is sufficient.

I assume the command for ogr2ogr could be something like:

ogr2ogr -f "ESRI Shapefile" -where "Y = 40.752459 AND X = -73.983521" output.shp input.shp

However I am having difficulties finding out what the attributes for lat/lon are named.

Optional: Also, is there an option for a fuzzy query where I can configure a radius around the coordinates?

I am running Ubuntu 12.04. with QGIS 1.9.0 via ubuntugis-nightly and GDAL 1.10.0.

1

I would not recommend using a single coordinate pair for accuracy reasons. For the fuzzy query, you can better create a buffer around that point.

Using QGIS, you can create points with the Numerical Digitize Plugin by coordinates, then create a buffer circle around the point with Vector -> Geoprocessing tools -> Buffer(s), and clip the shapefile layer to that buffer layer.

  • Sounds good. But how can I select a coordinate by typing in the latitude/longitude values? – JJD Jul 25 '13 at 10:17
  • The Numerical Digitize Plugin allows entering coordinates in a simple way. – AndreJ Jul 25 '13 at 11:06
  • I extracted NumericalDigitize 0.1.4 into .qgis2/python/plugins. It appears in the plugins list however it cannot be activated for QGIS 1.9.0. -- "This plugin is incompatible with this version of QGIS. Plugin designed for QGIS 1.5 - 1.99" – JJD Jul 26 '13 at 8:41
  • I managed to activate NumericalDigitize 0.1.4. by editing the version tag in metadata.txt and (optionally) __init.py__. It set qgisMinimumVersion=2.0. QGIS must be restarted to reload the modified plugin. – JJD Jul 26 '13 at 9:11
  • It worked for me in QGIS 1.8.0. You stumbled on the new plugin policy, which leads to problems until QGIS 2.0 is finally released. – AndreJ Jul 26 '13 at 10:10
1

An early solution with a PostGIS database (local installation).

If you want a radius you can use the ST_buffer function. 2 commands with ogr2ogr allow you to obtain your output shapefile :

1 -- load into PostGIS :

ogr2ogr -f "PostgreSQL" -a_srs "EPSG:4326" \
    PG:"host=127.0.0.1 user=postgres dbname=mydb password=mypwd" \
    input.shp -nln newtablename -nlt geometry -overwrite

2 -- execute query and export result with a radius --> st_buffer() 500 is the radius :

ogr2ogr -f  "Esri Shapefile" output.shp -a_srs "EPSG:4326" -s_srs "EPSG:4326" \
    PG:"host=127.0.0.1 user=postgres dbname=mydb password=mypwd" \
    -sql "select id_1, id_1 as name_field, id_1 as value_1_field, wkb_geometry \
    from newtablename, ( select  'id_x' as id, \
    setsrid(ST_buffer(ST_MakePoint(40.752459 , -73.98352),500),4326) as geom ) \
    as mypoint_with_radius where \
    st_intersects(newtablename.wkb_geometry, mypoint_with_radius.geom)" \
    PGCLIENTENCODING=LATIN1

I work on Windows 7 with FWTools but I can't test it on another machine.
I hope it will be useful ...

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.