4

I have been trying to figure out how to execute ogr2ogr to convert files in Python script. I got it to work in Jupyter notebook within ArcGIS Pro and in the terminal but not in Python scripts. Here's the code I used in the notebook.

import os,arcpy,shutil
from osgeo import ogr
abspath=os.path.abspath
abspath = os.path.abspath("__file__")
dname = os.path.dirname(abspath)
os.chdir(dname)
ProjectFolder=dname
arcpy.env.workspace=ProjectFolder
arcpy.env.overwriteOutput = True
! ogr2ogr siteboundary.shp Boundary.TAB
print("Conversion done,your file is located in "+ProjectFolder)

I searched a lot on how to use ogr commands in python script but had no luck. So I exported the notebook to a python script which was this.

import os,arcpy,shutil
from osgeo import ogr
from IPython import get_ipython
abspath=os.path.abspath
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
ProjectFolder=dname
arcpy.env.workspace=ProjectFolder
arcpy.env.overwriteOutput = True
get_ipython().System("! ogr2ogr siteboundary.shp boundary.geojson")
print("Conversion done, your file is located in "+ProjectFolder)

But this script is giving me a 'NoneType' error

"Traceback (most recent call last): File "S:\TOOLS\Scripts.test\CT429_ContamCertificates_GIS\Untitled1.py", line 24, in get_ipython().System("! ogr2ogr siteboundary.shp boundary.geojson") AttributeError: 'NoneType' object has no attribute 'System'"

So, I looked up nonetype error for system, found a really good resource "Any idea why IPython.get_ipython() is returning None?" but it's not working for me.

How can I do this?

I need this so that my team can run the Python scripts from the folder itself as that's more convenient.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 7, 2022 at 5:02
0

2 Answers 2

2

I usually use subprocess to execute ogr2ogr.exe in windows cmd/terminal.

From geojson to shape:

import subprocess
command = [r"C:\OSGeo4W\bin\ogr2ogr.exe", r"C:\GIS\data\lan.shp", r"C:\GIS\data\lan.geojson"]
output, error = subprocess.Popen(
 command, universal_newlines=True,
 stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
answered Mar 7, 2022 at 6:15
2
  • 1
    Thank you so much, this works. I changed the command to this 'command= [r"S:\TOOLS\Scripts\.test\ogr2ogr.exe", "siteboundary.shp", "geojson.json"]' It works! Yes, I even changed the location of ogr2ogr.exe file to a relative location that is accessible by my team as well, and it works :) Commented Mar 8, 2022 at 1:54
  • I still find it strange how the ogr2ogr functionality isn't available via the python library. We must be missing something? Commented Jul 22, 2022 at 17:57
1

This is how I did it (for fgb):

from osgeo import gdal
shpin = 'shpin.shp'
gdal.UseExceptions()
src_shp = gdal.OpenEx(shpin)
ds=gdal.VectorTranslate(
'abc.fgb',
src_shp,
options='-f "FlatGeobuf"'
)
del ds

All credit goes to these two threads:

Using ogr2ogr to convert GML to shapefile in Python?

gdal.VectorTranslate returns empty object

answered Feb 28, 2023 at 16:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.