Select to view content in your preferred language

How to get attributes using GPS coordinate from webmap

1447
9
10-30-2023 06:32 PM
Labels (2)
AENA
by
Emerging Contributor

Hi, currently i'm working with webmap and i want to find an attributes in the webmap by using GPS coordinate

output expected  : 

Location: Jalan Jabatan Perhutanan, Keningau, Bahagian Keningau, 89000, Sabah, MYS
KODDUN: N.04, NAMADUN: TANJONG KAPOR

output that i obtaint :

it print out all the attributes instead of the one that match with coordinate.

Location: Jalan Jabatan Perhutanan, Keningau, Bahagian Keningau, 89000, Sabah, MYS
KODDUN: N.04, NAMADUN: TANJONG KAPOR
KODDUN: N.03, NAMADUN: PITAS
KODDUN: N.05, NAMADUN: MATUNGGONG
KODDUN: N.06, NAMADUN: BANDAU
KODDUN: N.09, NAMADUN: TEMPASUK

 

my code : 

from arcgis.gis import GIS
import arcgis
from arcgis.geometry import Geometry
from arcgis.geocoding import reverse_geocode

gis = GIS()

webmap_item_id = "0c7d9a944a5a476694a2f998d53e3e1e"
webmap_item = gis.content.get(webmap_item_id)
webmap_data = webmap_item.get_data()

longitude = 116.157966
latitude = 5.341622
given_coord = {'x': longitude, 'y': latitude, "spatialReference": {"wkid": 4326}}
point_geom = Geometry(given_coord)

location_details = reverse_geocode([longitude, latitude])
place_name = location_details.get('address', {}).get('LongLabel', 'Unknown Location')
print(f"Location: {place_name}")

if 'operationalLayers' in webmap_data:
for layer in webmap_data['operationalLayers']:
if 'featureCollection' in layer:
for layer_collection in layer['featureCollection']['layers']:
for feature in layer_collection['featureSet']['features']:
attributes = feature['attributes']
geometry = feature['geometry']
koddun = attributes.get("KODDUN")
namadun = attributes.get("NAMADUN")
parlimen = attributes.get("PARLIMEN")

# Fetch GPS coordinates
if 'x' in geometry and 'y' in geometry:
longitude = geometry['x']
latitude = geometry['y']
print(f"KODDUN: {koddun}, NAMADUN: {namadun}, PARLIMEN: {parlimen}")
else:
print(f"KODDUN: {koddun}, NAMADUN: {namadun}")

 

hope someone can help me because this is my first time using API

0 Kudos
9 Replies
EarlMedina
Esri Regular Contributor

Hello,

If I understand what you are trying to do correctly, you are quite close and just need to add a geometry function to figure out if the input point is within the geometry. You may use Geometry (which will parse the type for you), but I will use Polygon and Point so it is more explicit what is going on. I'm not sure what the intention was with the else statement, so you may need to adjust further:

 

 

 

from arcgis.gis import GIS
import arcgis
from arcgis.geometry import Point, Polygon
from arcgis.geocoding import reverse_geocode

gis = GIS()

webmap_item_id = "0c7d9a944a5a476694a2f998d53e3e1e"
webmap_item = gis.content.get(webmap_item_id)
webmap_data = webmap_item.get_data()

longitude = 116.157966
latitude = 5.341622
given_coord = {"x": longitude, "y": latitude, "spatialReference": {"wkid": 4326}}
point_geom = Point(given_coord)

location_details = reverse_geocode([longitude, latitude])
place_name = location_details.get("address", {}).get("LongLabel", "Unknown Location")
print(f"Location: {place_name}")

if 'operationalLayers' in webmap_data:
    for layer in webmap_data['operationalLayers']:
        if 'featureCollection' in layer:
            for layer_collection in layer['featureCollection']['layers']:
                for feature in layer_collection['featureSet']['features']:
                    attributes = feature['attributes']
                    geometry = feature['geometry']
                    koddun = attributes.get("KODDUN")
                    namadun = attributes.get("NAMADUN")
                    parlimen = attributes.get("PARLIMEN")
                    # Fetch GPS coordinates
                    if "rings" in geometry:
                        poly_geom = Polygon(geometry)
                        if point_geom.within(poly_geom):
                            print(f"KODDUN: {koddun}, NAMADUN: {namadun}, PARLIMEN: {parlimen}")
                    else:
                        print(f"KODDUN: {koddun}, NAMADUN: {namadun}")

 

 

 

0 Kudos
AENA
by
Emerging Contributor

Hello, thankyou for the replied.

may I know if you get the expected output?

I've tried to run the code, but seems like it's not working correctly because I only get the location name but not the attributes(KODDUN, NAMADUN).

0 Kudos
EarlMedina
Esri Regular Contributor

Hey, I get this:

 

Location: Jalan Jabatan Perhutanan, Keningau, Bahagian Keningau, 89000, Sabah, MYS
KODDUN: N.40, NAMADUN: BINGKOR, PARLIMEN: KENINGAU
KODDUN: N.40, NAMADUN: BINGKOR, PARLIMEN: KENING 
0 Kudos
AENA
by
Emerging Contributor

Hai, I'm sorry if I ask you a lot of question because this is my first time using python API.

I've tried to run the code that you share, but I only get the location name. may I know why?

0 Kudos
AENA
by
Emerging Contributor

@EarlMedina 

@VinceAngelo 

can you help me with this one.

0 Kudos
EarlMedina
Esri Regular Contributor

The only thing that comes to mind is you may be on a differen version of the API? I re-ran the code and came out with the exact same output.

0 Kudos
AENA
by
Emerging Contributor

may i know what version and platform to run the code that you use ?

0 Kudos
AENA
by
Emerging Contributor

btw, I'm using google colaboratory to run the code. @EarlMedina 

 

0 Kudos
EarlMedina
Esri Regular Contributor

2.1.0 is the version, I believe.

0 Kudos