0

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?

1
  • You already have axes for plotting. Just re-use it, simply try axes.scatter(lon, lat) and tell us what you get. Commented Dec 3, 2020 at 11:07

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.