2

While creating a defaultdict(list) containing street names, x and y coordinates, my x and y entries are converted to strings. This prevents me from doing calculations on them. I've been looking around but can't find a quick fix.

Here's my code and the snippet of the data when I print it out :

import arcpy
import collections
import pandas as pd
import numpy as np
import statistics
from scipy.spatial import *

#Environments
arcpy.env.workspace = r'C:\Users\Contractuel\Documents\Etape_3'

#Local variables
fc = arcpy.env.workspace + '\SpatialJoin_Candiac.shp'
fields = ['NOMRTE','X','Y']

d = collections.defaultdict(list) 

with arcpy.da.SearchCursor(fc, fields) as cursor :        
    for roadname,x,y in cursor:            
        d[roadname].append([x,y])

Snippet :

Rang Saint-Andre:
[['617035,993369637', '5024323,46115644'], 
['616978,930218717', '5024342,95813809'], 
['616935,739846513', '5024366,96582001'], 
['616898,918096905', '5024394,63637209'], 
['616866,027555116', '5024438,9422574'], 
['616845,464712524', '5024486,52105514']]

Anybody has ever encountered this problem?

  • 6
    Need to ask the obvious question are the X and Y fields in SpatialJoin_Candiac.shp text or double? – Hornbydd Feb 20 at 21:22
  • 4
    I can't reproduce this with a shapefile that has numeric X & Y fields. Please edit your question to show us the field types, e.g. print('\n'.join(["%s: %s" % (f.name, f.type) for f in arcpy.Describe(fc).fields])) – user2856 Feb 20 at 21:31
  • 1
    Your xy fields must be strings. Replace last line with: d[roadname].append([float(x),float(y)]) or use the shape tokens to get actual geometries x and y centroid values as answer suggests – BERA Feb 21 at 5:58
1

It looks like you're running a SearchCursor over a spatial dataset (a shapefile).

You could be using the shapefile's built in geometry to get coordinate data, but instead you are using attribute fields as proxies for spatial data.

As others have guessed in the comments, it may be that those attributes are text fields, rather than numbers. In any case, it would make sense to use the build in geometry of features rather than attributes which were presumably calculated based on that geometry. Ie, why not go directly to the source? Those X,Y fields could become invalid if the actual geometry changes and the fields are not manually re-calculated.

Try this instead:

fields = ['NOMRTE','SHAPE@X','SHAPE@Y']`

Alternatively, you could use...

fields = ['NOMRTE','SHAPE@XY']`

...to get both coordinates at once, but would have to split it out later, something like...

for roadname,geom in cursor:            
    d[roadname].append([geom.X,geom.Y])

For reference, see the explanation for 'field_names' at either:

https://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm

or:

http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/searchcursor-class.htm

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.