Select to view content in your preferred language

Polyline in the wrong location with Arcpy and InsertCursor

518
4
04-04-2025 10:20 AM
mykola
by
Regular Contributor

Hello,

I experience issues in creating a polyline from the coordinates using arcpy. I am getting the data from the WFS server, truncating the table in my local geodatabase and inserting the data from the WFS server into the local geodatabase. We do not need to edit the data, just viewing and creating a dashboard.

The problem is the following: When I create a polyline geometry from the coordinate value pairs and insert into the target source using InsertCursor, the lines appear around the origin of the coordinate system (0,0) despite having the right coordinates. The funny thing is that the length of the lines is correct but they are centered around the (0,0) of the CS. 

I searched online, read a few posts here and there and used chatGPT but nothing works and I do not understand why. What confuses me is that I am actually copying the data from two layers on the WFS server using requests module and returning the JSON format, both are Multiline strings (with just one line actually) but one set of data is copied to the right location with the right geometry while the other one is placed around the origin (0,0). Both have identical coordinates and I even went line by line through the JSON file to check if the coordinates are the same. I also use the for-loop to get the data and insert it in the database so I am excluding that something is different for one feature service but not another. What a weird behavior... Anyone had similar experience and wants to hint how to fix it?

Here is the part of the code for this operation:

with arcpy.da.InsertCursor(target_layer, fieldnames) as cursor:
        for feature in data["features"]:
            # Get geometry and attributes from a feature
            geometry = feature["geometry"]
            attributes = feature["properties"]
            gml_id = feature["id"]
                                   
            # Create an array of points from geometry coordinates
            if geometry["type"] == "MultiLineString":
                array = arcpy.Array([arcpy.Point(*coords) for coords in geometry["coordinates"][0]])
                geom = arcpy.Polyline(array, spatRef, True, True)

            value = [geom] + [gml_id] + [attributes[field] for field in fieldnames[2:])
            cursor.insertRow(value)​

 

Tags (2)
0 Kudos
4 Replies
DavidPike
MVP Notable Contributor

Set the coordinate system of the target Feature Class to the same as the WFS.  You can Project to whatever you require after that.

mykola
by
Regular Contributor

Hi David,

thanks for the hint. The coordinate systems are the same in both sources. My workflow is to get the data with the Requests module, create a JSON, create the polyline objects for each feature and insert it in the local geodatabase using the InsertCursor. The issue I am facing is that out of four WFS layers, two point feature classes and two line feature classes, all but one line feature class fails to go into the right place. I would understand if the other line feature also ended in the wrong location but one if fine while the other is not. 

I thought maybe I should try using shapefile instead and copy the data into the geodatabase.

0 Kudos
Kimberly-Saballett
Esri Contributor

Are you able to provide the geometry portion of your json for testing? You said they're all the same coordinate system? Can you share which?

0 Kudos
mykola
by
Regular Contributor

Sure, I will send you in dm

0 Kudos