I found this helpful post that got me started using ogr2ogr in python: Using ogr2ogr to convert GML to shapefile in Python? as well as this one: Converting ArcGIS Server JSON to GeoJSON? I am trying to take JSON from arcgis service query results and convert to geojson. I am querying OBJECTIDs in increments of 1,000 as the query results limit is set to 1,000. I tested this and it works in command line, but when I run the script, I get an error saying unable to open data source. Can someone help?

import os
import ogr2ogr

def main():
    for i in range(0,83000,1000):
        url = ('https://gisrevprxy.seattle.gov/ArcGIS/rest/services/SDOT_EXT/sdot_parking/MapServer/5/query?'

        outfile = os.path.join('/Users/kirsten_lawrence/Documents','recs_'+str(i)+'up.json')
        # note: main is expecting sys.argv, where the first argument is the script name
        # so, the argument indices in the array need to be offset by 1
        print 'creating geojson starting at record',i
        ogr2ogr.main(['','-f', 'GeoJSON', outfile, url, 'OGRGeoJSON'])
  • I confirmed his url works with i=0 and i=2000. Also I'd never seen that multi-line string syntax before, but it checks out.. For sanity you might try invoking ogr2ogr command line programmatically, like this: os.system(ogr2ogr_terminal_instruction) My assumption is something must be wrong with how your submitting your arguments to ogr2ogr.main() – elrobis Jun 22 '16 at 14:33
  • Thanks for the comment, yes I can certainly do that, though I wish I could get this working. Is it pretty much equivalent to use os.system or subprocess.call? – kflaw Jun 22 '16 at 20:16

Your Answer

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

Browse other questions tagged or ask your own question.