Hi,
I wrote a python script that connects to Access database and loops through each row to get lat/long values and then creates features in a shapefile.
Note: I am using pyodbc library to connect to database. Earlier I did test with arcpy.da.Searchcursor but had other issues while implementing the gpk file from ArcGIS Runtime application.
The script runs fine from Python IDE when path to the database is hardcode. l
I've created a script tool from the above script file that takes parameters for the database path and shapefile path.
When I run it in ArcCatalog/ArcMap error is thrown randomly - sometimes before looping through the cursor, sometimes after looping through couple of rows otherwise at the end . This happens especially if I change the input database path .
Here is the python script -
import pyodbc,arcpy
#Database path: SiteName, SiteLatitude and SitesLongitude required
readpath = arcpy.GetParameterAsText(0)
#Path of the shapefile to add shapes. It should have the SiteName field required
writepath = arcpy.GetParameterAsText(1)
writeshapecursor = arcpy.da.InsertCursor(writepath, ("SiteName", "SHAPE@XY"))
connection = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb)};DBQ=' + readpath + ';')
cursor = connection.cursor()
for row in cursor.execute("Select * from Sites"):
x = row[4]
y = row[3]
value = row[2]
arcpy.AddMessage("Row value - " + value)
print("Row value - " + value)
newrow = (value, (x,y))
writeshapecursor.insertRow(newrow)
arcpy.SetParameterAsText(2,writepath)
del writeshapecursor
Following is the error when executed in ArcMap-
Executing: XYtShapepyodbc C:\\Users\\Admin\\Documents\\ArcGIS\\Tanz.mdb C:\\Users\\Admin\\Documents\\ArcGIS\\IML.shp
Start Time: Thu Feb 28 13:10:14 2013
Running script XYtShapepyodbc...
Row value - (GTZ) Family Health Project
ERROR 000714: Error in script XYtShapepyodbc.
Error in executing: cmd.exe /C C:\Users\Admin\DOCUME~1\ArcGIS\XYTOSH~1.PY "C:\\Users\\Admin\\Documents\\ArcGIS\\Tanz.mdb" "C:\\Users\\Admin\\Documents\\ArcGIS\\IML.shp" "C:\Users\Admin\Documents\ArcGIS\IML.shp"
Failed to execute (XYtShapepyodbc).
Failed at Thu Feb 28 13:10:24 2013 (Elapsed Time: 10.00 seconds)
Any ideas?
I wrote a python script that connects to Access database and loops through each row to get lat/long values and then creates features in a shapefile.
Note: I am using pyodbc library to connect to database. Earlier I did test with arcpy.da.Searchcursor but had other issues while implementing the gpk file from ArcGIS Runtime application.
The script runs fine from Python IDE when path to the database is hardcode. l
I've created a script tool from the above script file that takes parameters for the database path and shapefile path.
When I run it in ArcCatalog/ArcMap error is thrown randomly - sometimes before looping through the cursor, sometimes after looping through couple of rows otherwise at the end . This happens especially if I change the input database path .
Here is the python script -
import pyodbc,arcpy
#Database path: SiteName, SiteLatitude and SitesLongitude required
readpath = arcpy.GetParameterAsText(0)
#Path of the shapefile to add shapes. It should have the SiteName field required
writepath = arcpy.GetParameterAsText(1)
writeshapecursor = arcpy.da.InsertCursor(writepath, ("SiteName", "SHAPE@XY"))
connection = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb)};DBQ=' + readpath + ';')
cursor = connection.cursor()
for row in cursor.execute("Select * from Sites"):
x = row[4]
y = row[3]
value = row[2]
arcpy.AddMessage("Row value - " + value)
print("Row value - " + value)
newrow = (value, (x,y))
writeshapecursor.insertRow(newrow)
arcpy.SetParameterAsText(2,writepath)
del writeshapecursor
Following is the error when executed in ArcMap-
Executing: XYtShapepyodbc C:\\Users\\Admin\\Documents\\ArcGIS\\Tanz.mdb C:\\Users\\Admin\\Documents\\ArcGIS\\IML.shp
Start Time: Thu Feb 28 13:10:14 2013
Running script XYtShapepyodbc...
Row value - (GTZ) Family Health Project
ERROR 000714: Error in script XYtShapepyodbc.
Error in executing: cmd.exe /C C:\Users\Admin\DOCUME~1\ArcGIS\XYTOSH~1.PY "C:\\Users\\Admin\\Documents\\ArcGIS\\Tanz.mdb" "C:\\Users\\Admin\\Documents\\ArcGIS\\IML.shp" "C:\Users\Admin\Documents\ArcGIS\IML.shp"
Failed to execute (XYtShapepyodbc).
Failed at Thu Feb 28 13:10:24 2013 (Elapsed Time: 10.00 seconds)
Any ideas?