2

I am a beginner in Python.

I have a database in .accdb format of Access 2007. The database has a table called "T_point_RT" which has point data.

I want to create a point shapefile from North and Est coordinates (in T_point_RT table) in a way that each record in the shapefile corresponds each point. The shapefile must have also the ID field for each point.

This is the script, the problem is that the shapefile that is created has only one record/one point (the last one). What is the problem ??

import arcpy
import pyodbc
import numpy
import datetime
data=datetime.date.today().strftime('%Y%m%d')
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Python_Elaborazioni\Dati\Database\DB_COP_Field_RedTeam.accdb;')
cursor = conn.cursor()
for row in cursor.execute('''select ID_point, Est, North from T_point_RT'''):
 array = numpy.array([(row[0], (row[1], row[2]))],
 numpy.dtype([('Id_Point',numpy.unicode_,6),('XY', 'f8',2)]))
 SR = arcpy.SpatialReference(32632)
 SHP_WGS84= "C:/Python_Elaborazioni/Dati/Shp/P_DB_COP_Field_RedTeam_"+ data +" _wgs.shp"
arcpy.da.NumPyArrayToFeatureClass(array, SHP_WGS84, ['XY'], SR)
outFC_MM = "C:/Python_Elaborazioni/Dati/Shp/P_DB_COP_Field_RedTeam_"+ data +"_gb.shp"
SR_MM = arcpy.SpatialReference('Monte Mario Italy 1')
arcpy.Project_management(SHP_WGS84,outFC_MM,SR_MM)
print "Finito"
asked Jun 3, 2018 at 15:26
2
  • You're overwriting your array on each iteration so only the last point is in it. Have a read of stackoverflow.com/questions/9775297/… about appending an array to an existing array.. create your array on your first iteration (array == None) then create a separate array (sarray = numpy.array(...) and concatenate array = numpy.append(array,sarray) so at the end of your cursor.execute loop you will have an array with all the values in it. Commented Jun 3, 2018 at 23:39
  • @MichaelStimson please read my answer. Commented Jun 4, 2018 at 20:20

1 Answer 1

2

This works perfectly after help from Michael Stimson.

import arcpy
import pyodbc
import numpy
import datetime
data=datetime.date.today().strftime('%Y%m%d')
conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Python_Elaborazioni\Dati\Database\DB_COP_Field_RedTeam.accdb;')
cursor = conn.cursor()
array = None
for row in cursor.execute('''select ID_point, Est, North from T_point_RT'''):
 if array == None:
 array = numpy.array([(row[0], (row[1], row[2]))],
 numpy.dtype([('Id_Point',numpy.unicode_,6),('XY', 'f8',2)]))
 else:
 sarray = numpy.array([(row[0], (row[1], row[2]))],
 numpy.dtype([('Id_Point',numpy.unicode_,6),('XY', 'f8',2)]))
 array = numpy.append(array,sarray)
SR = arcpy.SpatialReference(32632)
SHP_WGS84= "C:/Python_Elaborazioni/Dati/Shp/P_DB_COP_Field_RedTeam_"+ data +" _wgs.shp"
arcpy.da.NumPyArrayToFeatureClass(array, SHP_WGS84, ['XY'], SR)
outFC_MM = "C:/Python_Elaborazioni/Dati/Shp/P_DB_COP_Field_RedTeam_"+ data +"_gb.shp"
SR_MM = arcpy.SpatialReference('Monte Mario Italy 1')
arcpy.Project_management(SHP_WGS84,outFC_MM,SR_MM)
print "Finito"
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jun 4, 2018 at 22:43

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.