Big Face
Big Face

Reputation: 53

Plot points from a csv file onto a geopandas map

In the first step, I have already created a plot from polygon shapefile, gpd here refers to the geopandas, the code is like below:

data = gpd.read_file('data.shp')
fig, axes = plt.subplots(figsize=(10,8))
data.plot(ax = axes, color = "white", edgecolor = "black")
plt.plot()

After this, I have a polygon plot, now I have another csv file containing coordinate information, I want to plot these points onto the previous polygon plot, I try code as below to access the coordinate information:

lat=[]
lon=[]
with open('data.csv') as csvfile:
    reader = csv.reader(csvfile,delimiter=',')
    for data in reader:
        lat.append(float(data[2]))
        lon.append(float(data[3]))

I check the lat and lon, no problem, but what should I do next so that I can show both points and polygon at the same time?

Upvotes: 0

Views: 3972

Answers (1)

James_SO
James_SO

Reputation: 1387

You need to be aware of and harmonize the CRS (Coordinate Reference System) being used in both the shape file and the .csv

You can likely get the crs of the shape file with

    data.crs

Let's say your output is something like this:

<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

So the CRS of the shape file is 4326 - now you have to figure out the CRS of the .csv, or maybe the most likely CRS. You could get that by searching generally, or use a reference like this: https://www.nceas.ucsb.edu/sites/default/files/2020-04/OverviewCoordinateReferenceSystems.pdf

Let's say you review that .pdf and decide that this sounds like your .csv:

Projected (Easting/Northing) UTM, Zone 10 (EPSG: 32610) Zone 10 is used in the Pacific Northwest

So you'd need to first initialize your data with 32610, then reproject it to 4326

    from shapely.geometry import Point
    from geopandas import GeoDataFrame
    import pandas as pd

    df = pd.read_csv("data.csv")
    geometry = [Point(xy) for xy in zip(df[2], df[3])]
    gdf = GeoDataFrame(df, geometry=geometry, crs= 32610) 
    gdf = gdf.to_crs(4326)

Upvotes: 2

Related Questions