To learn geopandas, I'm trying to layer US cities (raw json file) over a world map provided by Geopandas. With the same CRS, the US cities are showing up in the wrong place (picture below). If I used the cities dataset (provided by Geopandas.datasets), it works fine.

import matplotlib.pyplot as plt
from geopandas import GeoDataFrame
from shapely.geometry import Point
import pandas as pd

world = world[(world.pop_est>0) & (world.name!="Antarctica")]
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est

# US Cities from Raw JSON File
url = 'https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json'
cities_df = pd.read_json(url)
geometry = [Point(xy) for xy in zip(cities_df['latitude'], cities_df['longitude'])]
cities_gpd = GeoDataFrame(cities_df, crs=world.crs, geometry=geometry)

cities_gpd.crs, world.crs
# Ouputs ({'init': 'epsg:4326'}, {'init': 'epsg:4326'})

fig, ax = plt.subplots(figsize=(10, 5))
world.plot(ax=ax, column='gdp_per_cap', cmap='OrRd', scheme='quantiles')
cities_gpd.plot(ax=ax, marker='o', color='green', markersize=16)

Map Image enter image description here


The problem appears to be here:

geometry = [Point(xy) for xy in zip(cities_df['latitude'], cities_df['longitude'])]

Latitude is y, longitude is x. Just swap the keys.

You can see the transposition in the image below.

geo flipped

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.