Select to view content in your preferred language

Mapping local shapefile overlayed with Living Atlas layer

130
2
a week ago
Labels (3)
HieuTran12
Emerging Contributor

I've been struggling with mapping the overlaying of local shapefile with Living Atlas layers. 

I was able to map it on the AGOL UI, but with Python API I'm stuck.

My approach is converted the shapefile to spatial dataframe enabled and plot to the map widget where is the Living Atlas layer already mapped. But the shapefile is not mapped. I tried to reproject the sedf to match with the basemap but still no luck (when I mapped this on AGOL UI, I don't have to reproject my shapefile)

 

Code to reproduce:

shp_df = pd.DataFrame.spatial.from_featureclass(location = "./data/PR_block_2020.shp", sr = 3857)

gis = GIS()

lecz_layer = gis.content.search("Low Elevation Coastal Zones derived from MERIT-DEM", item_type="Imagery Layer")
for item in lecz_layer:
    display(item)
lecz = lecz_layer[0]

m1 = gis.map("Puerto Rico")
m1.content.add(lecz)
shp_df.spatial.plot(map_widget=m1, opacity=0.7)

 

 

Tags (4)
0 Kudos
2 Replies
Clubdebambos
MVP Regular Contributor

Hi @HieuTran12,

Are you using a Jupyter Notebook in ArcGIS Pro?

Try this...

from arcgis.gis import GIS
import pandas as pd

## Access ArcGIS Online
gis = GIS("home")

## path to the shapefile
shp = r"C:\PATH\TO\MY.shp"

## get the item from Living Atlas
lecz = gis.content.get("87f875a0e4ac4400bad9063c18520f9a")

## get the shp as df
df = pd.DataFrame.spatial.from_featureclass(location=shp, sr=3857)

## convert the df to a feature collection
fc = df.spatial.to_feature_collection(name = "PR Block 20202", sanitize_columns="True")

## create a map widget
map_widget = gis.map("Puerto Rico")

## add the layers
map_widget.content.add(lecz.layers[0])
map_widget.content.add(fc)

## display the map
map_widget

Here's my example with counties in Ireland.

Clubdebambos_0-1742804993671.png

 

All the best,

Glen

~ learn.finaldraftmapping.com
0 Kudos
WilliamCooper
New Contributor

Your approach is correct, but the issue might be related to the spatial reference. Try explicitly setting the coordinate system of your spatially enabled DataFrame (sedf) to match AGOL’s Web Mercator (EPSG:3857). Modify your code as follows:

from arcgis.gis import GIS
from arcgis.features import GeoAccessor, GeoSeriesAccessor
import pandas as pd

# Load shapefile and reproject
shp_df = pd.DataFrame.spatial.from_featureclass(location="./data/PR_block_2020.shp")
shp_df.spatial.set_geometry("SHAPE") # Ensure geometry is set
shp_df.spatial.project(3857) # Reproject to match AGOL basemap

# Initialize GIS and get Living Atlas layer
gis = GIS()
lecz_layer = gis.content.search("Low Elevation Coastal Zones derived from MERIT-DEM", item_type="Imagery Layer")[0]

# Create map and add layers
m1 = gis.map("Puerto Rico")
m1.add_layer(lecz_layer)
shp_df.spatial.plot(map_widget=m1, opacity=0.7)

m1

0 Kudos