如何限制使用 GeoPandas 读取的特征数量?

How to limit the number of features read in using GeoPandas?

我有以下 Python 代码使用点 x, y.

将我的 shapefile 特征读入 GeoDataFrame
import math
import shapely.geometry
import geopandas as gpd
from shapely.ops import nearest_points

absolute_path_to_shapefile = 'c:/test/test1.shp'

gdf1 = gpd.read_file(absolute_path_to_shapefile)
gdf = gpd.GeoDataFrame(
    gdf1, geometry=gpd.points_from_xy(gdf1['x'], gdf1['y']))

有没有办法限制读入的特征?一些 shapefile 有数百万个点,但我只想阅读前 100 个作为概念证明。

GeoPandas read_file() 有一个 rows 选项来限制读取的行数(或使用切片读取特定行)。

import math
import shapely.geometry
import geopandas as gpd
from shapely.ops import nearest_points

absolute_path_to_shapefile = 'c:/test/test1.shp'

gdf1 = gpd.read_file(absolute_path_to_shapefile, rows=100)
gdf = gpd.GeoDataFrame(gdf1, geometry=gpd.points_from_xy(gdf1['x'], gdf1['y']))

GeoPandas documentation

geopandas.read_file(filename, bbox=None, mask=None, rows=None, **kwargs) Returns a GeoDataFrame from a file or URL.

Parameters
filename: str, path object or file-like object
Either the absolute or relative path to the file or URL to be opened, or any object with a read() method (such as an open file or StringIO)

bbox: tuple | GeoDataFrame or GeoSeries | shapely Geometry, default None
Filter features by given bounding box, GeoSeries, GeoDataFrame or a shapely geometry. CRS mis-matches are resolved if given a GeoSeries or GeoDataFrame. Tuple is (minx, miny, maxx, maxy) to match the bounds property of shapely geometry objects. Cannot be used with mask.

mask: dict | GeoDataFrame or GeoSeries | shapely Geometry, default None
Filter for features that intersect with the given dict-like geojson geometry, GeoSeries, GeoDataFrame or shapely geometry. CRS mis-matches are resolved if given a GeoSeries or GeoDataFrame. Cannot be used with bbox.

rows: int or slice, default None
Load in specific rows by passing an integer (first n rows) or a slice() object.

**kwargs :
Keyword args to be passed to the open or BytesCollection method in the fiona library when opening the file. For more information on possible keywords, type: import fiona; help(fiona.open)

Returns
geopandas.GeoDataFrame or pandas.DataFrame :
If ignore_geometry=True a pandas.DataFrame will be returned.