I am reading a shapefile as geopandas DataFrame
and them using pandas subset method to select a region.
geodata = gpd.read_file(bayshp)
geodata.dtypes
geodata.head(10)
OBJECTID FIPSSTCO COUNTY geometry
1 06001 Alameda (POLYGON ((6065941.393835935 2104148.464510527...
2 06013 Contra Costa (POLYGON ((6143913.640835938 2209458.230510532...
3 06041 Marin (POLYGON ((5879149.417835938 2203020.920510533...
4 06055 Napa POLYGON ((6075700.362835937 2441916.530510533,...
5 06075 San Francisco (POLYGON ((5990480.312835939 2123810.13351053,..
Subset the df:
# Subset based on the index
geosub = geodata.iloc[0:2]
I've got a function that accepts geopandas DataFrame
and number of points to sample as arguments.
def sample_random_geo(df, n):
# Randomly sample geolocation data from defined polygon
points = np.random.sample(df, n)
return points
However, the np.random.sample
or for that matter any numpy random sampling doesn't support geopandas object type.
I am wondering if there is a way to randomly sample geocoordinates from the spatial region.
Best Answer
Here's another way to do it:
It looks like the following:
Now you have: