GeoJSON – Download GeoJSON from URL with Parameters Using ogr2ogr

geojsonogr2ogr

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
FAILURE:
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.

Best Answer

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 \
        'https://data.ny.gov/resource/9a8c-vfzj.geojson?$limit=100000&$where=county%20in(%27Kings%27,%27Queens%27,%27Bronx%27,%27Richmond%27,%27New%20York%27)'

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