I'm trying to map some lat/long data transformed into shapely.geometry.Point objects from a geopandas.GeoDataFrame onto a UK shapefile downloaded from here . Upon extraction there are 3 .shp files and the problem below occurs with each of them.
Following is my code:
import geopandas as gpd
geometry = [Point(xy) for xy in zip(df['longitude'], df['latitude'])]
crs = {'init': 'epsg:4326'}
geo_df = gpd.GeoDataFrame(df, crs=crs, geometry=geometry)
ukmap = gpd.read_file("..\\gb_1km.shp")
fig, ax = plt.subplots(figsize=(6,6))
ukmap.plot(ax=ax) #check map
geo_df.plot(ax=ax, markersize=20, color='blue', marker = 'o', label = 'C')
Output looks like this:
Without the background shapefile the plot of the geometry looks like this:
Why is this happening and how could I fix it? Thanks
EDIT: With epsg = 27700
Answering in a bit more detail as this seems clear enough...
In your code generating geo_df
, you specify a crs
of 'epsg:4326'
, and based on your code, you are working in degree latitudes and longitudes. This is confirmed by looking at EPSG 4326 , which specifies the bounds of the CRS as [-180, -90, 180, 90].
I downloaded and read in the file you linked. This code:
uk_10km = gpd.read_file(r'/Users/brendancox/Downloads/Great_Britain_shapefile/gb_10km.shp')
uk_10km.crs
returns
{'init': 'epsg:3035'}
EPSG 3035 indicates that its units are in metres. The WGS84 bounds are [-10.6700, 34.5000, 31.5500, 71.0500], but the projected bounds are [2426378.0132, 1528101.2618, 6293974.6215, 5446513.5222].
Thus when you plot geo_df
onto the UK shapefile, it aligns to the ukmap
crs
, placing it in the bottom corner.
Given that EPSG 4326 is for the entire globe, and you are focusing on the UK, I would recommend using a UK-specific projection. EPSG 3035 appears to be for all of Europe, so you could probably find a UK-specific projection into which to convert both of your shape files, using geopandas.GeoDataFrame.to_crs()
.
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
world.to_crs(epsg='3035', inplace=True)
fig, ax = plt.subplots(figsize=(10,8))
world.plot(ax=ax)
cities.plot(ax=ax, color='red')
plt.show()
This shows how re-projecting the built-in world
shape to EPSG 3035 increases the scale, and makes the cities
shape appear in a small cluster near (0, 0) -- in this case, at the center of the map.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.