Trying to get X,Y and Z from hosted feature layer in portal using the following ArcGIS Api for python. The return is only for X and Y and no Z.
Any idea if there is another way thanks.
portal = "link to portal"
u = "user"
p = "password"
gis = GIS(portal,u,p)
# Specify the URL of your feature layer
feature_layer_url = "link to layer"
feature_layer = FeatureLayer(feature_layer_url)
features = feature_layer.query(where="objectid= 1", out_fields="*").features
# Extract coordinates
for feature in features:
geometry = feature.geometry
print(geometry )
the return is
Geometry: {'x': 477361.3940345794, 'y': 1100421.3303648233, 'spatialReference': {'latestWkid': 2882, 'wkid': 2882}}
I want to note that the feature has Z.
Hi Aymen,
There is a tool called Calculate Geometry Attributes that I find useful, this allows me to update/populate x,y and z values to fields I have created in the Feature Service. You can definitely take this data and create a table from it.
I use it two ways:
First way- is having a layer with fields where you want the data stored, then pull up the Calculate Geometry Attributes tool. Make sure your layer is in an ArcGIS Pro project then add it and set your fields and properties for which values are being calculated. I run this daily using scheduler on ArcGIS Pro but you can run it once too.
Second way- is modelbuilder if you have more stuff to add, like exporting the feature layer to a table. In my case, I have to do additional field calculations because the z values here are collected in meters and I calculate to them to feet. This is also running daily. Again, make sure you have the feature layer in the ArcGIS Pro project.
I also use AI (Chat GPT and Mircrosoft CoPilot) when I need help searching for something specific and I cannot find the answer I need. I took your script sent it in and asked to also show z values and here is the first response I received. It could be a start but I would test it out, maybe manipulate it a little.
from arcgis.gis import GIS
from arcgis.features import FeatureLayer
# Connect to the GIS portal
portal = "link to portal"
u = "user"
p = "password"
gis = GIS(portal, u, p)
# Specify the URL of your feature layer
feature_layer_url = "link to layer"
feature_layer = FeatureLayer(feature_layer_url)
# Query the feature layer
features = feature_layer.query(where="objectid=1", out_fields="*").features
# Extract and print coordinates including the z value
for feature in features:
geometry = feature.geometry
if 'z' in geometry:
z_value = geometry['z']
print(f"Coordinates: {geometry['x']}, {geometry['y']}, Z: {z_value}")
else:
# If z value is embedded in the coordinates
if "hasZ" in geometry and geometry["hasZ"]:
coordinates = geometry['paths'] if 'paths' in geometry else geometry['rings']
for path in coordinates:
for point in path:
x, y, z = point
print(f"Coordinates: {x}, {y}, Z: {z}")
else:
print(f"Coordinates: {geometry['x']}, {geometry['y']}")
@Aymen_Farhat in case you didn't already find the solution, you have to add return_z=True to your query. I had this question too and finally figured it out.
Here is your edited query that should return z values.
features = feature_layer.query(where="objectid= 1", out_fields="*",return_z=True).features