Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert the coordinates of a shapefile in Geopandas

I am new at spatial analysis, but I cant find this answer anywhere.

I have a list of post codes in CRS coordinates, latitude and longitude, and London's Boroughs shape file in OSN coordinates, and I would like to map them together, but this is what happens. This is the head of the postcodes

london_post_codes.head()
Out[81]:
postcode    latitude    longitude
0   WD6 1GS 51.658021   -0.255663
1   WD17 1LA    51.660366   -0.397525
2   WC2N 6LE    51.509413   -0.121676
3   WC2N 6NA    51.508363   -0.124454
4   WC2N 6ND    51.508216   -0.123829

while this is the shape file read in geopandas

borough = gpd.read_file('London_Borough_Excluding_MHW.shp')
borough.head()
borough.head()

NAME    GSS_CODE    geometry
0   Kingston upon Thames    E09000021   POLYGON ((516401.6 160201.8, 516407.3 160210.5...
1   Croydon E09000008   POLYGON ((535009.2 159504.7, 535005.5 159502, ...
2   Bromley E09000006   POLYGON ((540373.6 157530.4, 540361.2 157551.9...
3   Hounslow    E09000018   POLYGON ((521975.8 178100, 521967.7 178096.8, ...
4   Ealing  E09000009   POLYGON ((510253.5 182881.6, 510249.9 182886, ...

we can see how the coordinates of the polygons are different from those of the postcodes. I and when I plot them together I get

fig, ax = plt.subplots()
borough.plot(ax = ax)
borough = gpd.read_file('statistical-gis-boundaries-london/ESRI/London_Borough_Excluding_MHW.shp')
london_post_codes.plot(kind='scatter',s=10, x='longitude', y='latitude',ax=ax)

enter image description here

Any suggestions ?

like image 403
Duccio Piovani Avatar asked Oct 15 '25 23:10

Duccio Piovani


1 Answers

The solution is simply to change the CRS (Coordinate Reference System). These go by codes, which are called EPSG. The WSG84 lat/long CRS has a EPSG code of 4326. Therefore

borough = gpd.read_file('London_Borough_Excluding_MHW.shp')
borough = borough.to_crs(epsg=4326)

and then the rest.

like image 91
Duccio Piovani Avatar answered Oct 18 '25 12:10

Duccio Piovani