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.