2

I have a .csv file with longitude, latitude representing a point on map. And another column that I will use as weights for these maps. I want to calculate weighted mean center for all the points using ArcPy.

This is what I've tried so far:

import pandas as pd
import arcpy


df = pd.read_csv("data.csv")
longs = df['Longitude'].tolist()
lats = df['Latitude'].tolist()
wts = df['Weights'].tolist()
point = arcpy.Point()
pointGeoms = []

for idx in range(len(longs)):
    point.X = float(lats[idx])
    point.Y = float(longs[idx])
    point.M = int(wts[idx])
    pointGeoms.append(arcpy.PointGeometry(point))
arcpy.CopyFeatures_management (pointGeoms,"centroids.shp")

Above code is for generating shapefile. Below code is for calculating weighted means.

import arcpy


workspace = r"C:\Users\sam"
input_FC = "centroids.shp"
weight_field = "M"

try:
    # Set the workspace to avoid having to type out full path names
    arcpy.env.workspace = workspace
    # Process: Mean Center...
    arcpy.MeanCenter_stats(input_FC, MEAN_output, "#", "#", "#")

except:
    # If an error occurred when running the tool, print out the error message.
    print(arcpy.GetMessages())

I am not able to get any positive outcomes from both the codes. The shapefile generated by first code shows empty point values in ArcGIS Pro. And the second code generates a point randomly placed very far away from any point of interest.

3
  • You mention ArcGIS Pro but do not have a tag for that, and do have a tag for ArcMap which you do not mention. It may not matter but which architecture are you using?
    – PolyGeo
    Commented Dec 20, 2017 at 2:58
  • Hi, I am using ArcGIS Pro Desktop. There was no tag for ARCGIS Pro when I added this question.
    – SirPunch
    Commented Dec 21, 2017 at 4:04
  • It's the ArcGIS Pro application of ArcGIS Desktop, and there has been an arcgis-pro tag available for it since 16 May 2014.
    – PolyGeo
    Commented Dec 21, 2017 at 4:15

1 Answer 1

1

Remember that latitude is actually the Y axis, so if you are entering the coordinates, it should be long, lat not lat, long. You should also create a weight field for the feature, not for the geometry.

This is a working example:

import arcpy

lats = [0, 1, 2]
longs = [1, 3, 4]
wts = [1, 3, 5000]

pointGeoms = []
feature_class = arcpy.CreateFeatureclass_management(
    "in_memory", "tempfc", "POINT")[0]
arcpy.AddField_management(feature_class, "weight", "LONG")

with arcpy.da.InsertCursor(feature_class, ["weight", "SHAPE@XY"]) as cursor:
    for i in range(len(longs)):
        cursor.insertRow([wts[i], (longs[i], lats[i])])
try:
    arcpy.MeanCenter_stats(feature_class, 'c:\\temp\\mean.shp', "weight", "#", "#")

except:
    # If an error occurred when running the tool, print out the error message.
    print(arcpy.GetMessages())
3
  • Thank you, Lennert! Your working example is really helpful. On a parting note, do you know any good book/course that you'd recommend for learning ArcGIS Python programming?
    – SirPunch
    Commented Dec 21, 2017 at 4:12
  • Hi, sorry, I can't help you there, I learn everything via the arcpy documentation on esri and gis.stackexchange Commented Dec 21, 2017 at 4:25
  • 1
    @LennertDeFeyter I think you just gave the best resources on the subject. Particularly Geographic Information Systems.
    – Fezter
    Commented Dec 21, 2017 at 4:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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