0

I have a csv file in which the 2nd and 3rd rows have lat and long values. The CSV file contains the temperature data from 2011 to 2099 in India, and I want to filter data for only the Satulaj basin using the shapefile of the Satulaj basin. How do I do this in python.

import shapefile
from shapely.geometry import shape, Point
import pandas as pd


path="D:\\THESIS\\Others\\DharamVeer_Sir\\1_Future_Climate Data\\"
df = pd.read_csv(path+"test3.csv")


path1 = "D:\\THESIS\\Others\\DharamVeer_Sir\\satulaj (1)\\Satluj\\"
# read your shapefile
r = shapefile.Reader(path1+"satulaj.shp")

# get the shapes
shapes = r.shapes()

# build a shapely polygon from your shape
polygon = shape(shapes[0])    

def check(lon, lat):
    # build a shapely point from your geopoint
    point = Point(lon, lat)

    # the contains function does exactly what you want
    return polygon.contains(point)

for i in range(len(df.axes[1])):
    sfile = df.values[0][i]
    dst = df.values[1][i]
    print(check(sfile,dst))
monu
  • 11
  • 2
  • Can you show us the code you're using now? Please edit your question and include the code. – Amir Shabani Jun 18 '22 at 05:10
  • Are you using Pandas for this? If not, you can try using Pandas to filter a CSV file. And then you can ask a more detailed question, preferably including the code you used, a small sample data, and possible errors that you got. – Amir Shabani Jun 18 '22 at 05:17
  • 1
    Check this out: https://stackoverflow.com/q/7861196/246801. That’s not about a CSV specifically, but the relevant part is the geo point and the shape file. – Zach Young Jun 18 '22 at 05:28
  • I have now modified the program but it's giving false as output. for all points' – monu Jun 28 '22 at 07:43

0 Answers0