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"
  • 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. – Michael Stimson Jun 3 '18 at 23:39
  • @MichaelStimson please read my answer. – Michele Papasidero Jun 4 '18 at 20:20
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"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.