Layering maps with different axes

Viewed 70

I am new to python, trying to create a map using geopandas with two layers. One layer is a shapefile of the city of Portland's neighborhoods, the other is a data set with latitudes and longitudes in Portland that I want to pin.

Each plots fine on its own (i.e. I've gotten that far), but layering doesn't work because the coordinates used for the city of Portland map aren't in latitude longitude form. There are instructions on Portland's open data website for how to use QGIS to convert a shapefile to a geoJSON, and in doing so setting the CRS to EPSG:4269, but I don't know how to do this in Python with the shapefile.

Images show each plotting fine until I try to plot them together, note the axes...

fig,ax = plt.subplots(figsize = (15,15))
street_map.plot (ax = ax, alpha = .4, color ='grey')

Portland shapefile

fig,ax = plt.subplots(figsize = (15,15))
#street_map.plot (ax = ax, alpha = .4, color ='grey')
geo_df[geo_df['price']>250].plot(ax=ax, markersize = 20, color = 'blue', marker = "o")

pins without borders

fig,ax = plt.subplots(figsize = (15,15))
street_map.plot (ax = ax, alpha = .4, color ='grey')
geo_df[geo_df['price']>250].plot(ax=ax, markersize = 20, color = 'blue', marker = "o")

failed layering because axes don't match

2 Answers

I have looked for data you reference, but only find geojson that are in consistent CRS (EPSG:4326). If you have a GeoDataFrame in another CRS, as per other comment, use to_crs("EPSG:4326") to make geometries consistent.

import requests
import geopandas as gpd
import plotly.express as px

# boundaries
gdf_b = gpd.GeoDataFrame.from_features(
    requests.get(
        "https://www.portlandmaps.com/arcgis/rest/services/Public/COP_OpenData_Boundary/MapServer/3/query?outFields=*&where=1%3D1&f=geojson"
    ).json()
)
# water fountains
gdf_wf = gpd.GeoDataFrame.from_features(
    requests.get(
        "https://opendata.arcgis.com/datasets/070f61b58ac646b0aaf2a223b2dd16fa_84.geojson"
    ).json()
)


px.scatter_mapbox(
    gdf_wf, lon=gdf_wf.geometry.x, lat=gdf_wf.geometry.y, hover_data=["COMMENTARY"]
).update_layout(
    mapbox={
        "style": "carto-positron",
        "layers": [{"source": gdf_b.__geo_interface__, "type": "fill", "opacity": 0.3}],
    }
)

enter image description here

Related