I have this working URL query for this dataset

https://data.ny.gov/resource/9a8c-vfzj.geojson?$limit=100000&$$app_token=mytoken&$where=county in('Kings','Queens','Bronx','Richmond','New York')

Now I want to pull it and save it to a GeoJSON with ogr2ogr

ogr2ogr -f "GeoJSON" /home/thefile.geojson "https://data.ny.gov/resource/9a8c-vfzj.geojson?$limit=100000&$$app_token=mytoken&$where=county in('Kings','Queens','Bronx','Richmond','New York')" OGRGeoJSON -s_srs EPSG:4326 -progress

ERROR 1: HTTP error code : 400
Unable to open datasource `https://data.ny.gov/resource/9a8c-vfzj.geojson?=100000&39457app_token=mytoken&=county in('Kings','Queens','Bronx','Richmond','New York')' with the following drivers.

the URL query works when I put it in the browser so I am wondering what the problem could be. I took out New York too see if it was the spaces in the URL being executed from the CMD but still no luck.

  • It seems like $ signs are interpreted to have some special meaning. Try to escape them as \$. For more detailed messages add --debug on --config CPL_DEBUG on to your command.
    – user30184
    Aug 31 at 20:24
  • If the url is already giving you geojson, why do you need ogr2ogr?
    – jbalk
    Aug 31 at 20:53
  • @jbalk was going to pipe it into postgres
    – ziggy
    Aug 31 at 20:57

You will find a working sample code below to directly get the data from the URL and push it to a PostGIS table (I've considered you already have an existing database and ran CREATE EXTENSION postgis;

ogr2ogr \
        -f "PostgreSQL" PG:"host=localhost dbname=yourdbname user=yourusername password=yourpassword" \
        -lco OVERWRITE=yes \
        -lco SCHEMA=public \
        -gt 65536 \
        --config PG_USE_COPY YES \
        -lco GEOMETRY_NAME=geom \
        -nln your_wanted_table_name_in_postgis \

PS: Tested on Linux, with Bash. Single quotes around remote url are important to avoid escaping $ character with \$.

  • thanks this worked well
    – ziggy
    Aug 31 at 23:18

I got this error when I tried to use -s_srs without also using -t_srs:

ERROR 5: if -s_srs is specified, -t_srs and/or -spat_srs must also be specified.

I was able to save a geojson file by using this:

ogr2ogr -f "GeoJSON" test.geojson "https://data.ny.gov/resource/9a8c-vfzj.geojson?$limit=100000&$where=county%20in(%27Kings%27,%27Queens%27,%27Bronx%27,%27Richmond%27,%27New%20York%27)" OGRGeoJSON -s_srs EPSG:4326 -t_srs EPSG:4326 -progress

Please note that I removed the apptoken from the url because I don't have one.

  • It could be a bit more effective to use /vsicurl/"https://data..." erouault.blogspot.com/2012/05/….
    – user30184
    Aug 31 at 21:17
  • No need for -s_srs EPSG:4326 or -t_srs EPSG:4326: your data is already EPSG:4326
    – ThomasG77
    Aug 31 at 21:39
  • i got a similar error to mine with your code...but noted with the escape characters
    – ziggy
    Aug 31 at 23:18

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.