如何限制使用Geopandas读取的功能数量?

发布于 2025-01-25 16:12:27 字数 490 浏览 3 评论 0原文

我有以下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']))

有没有办法限制读取的功能?一些ShapeFiles有数百万分,但我只想在前100个概念证明中阅读。

I have the following Python code to read my shapefile features into a GeoDataFrame using the points x, y.

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']))

Is there a way to limit the features read in? Some shapefiles have millions of points but I just want to read in the first 100 as proof of concept.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮惑 2025-02-01 16:12:27

geopandas read_file()具有选项,可以限制读取的行数(或使用切片来读取特定行)。

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.read_file(文件名,bbox = none,mask = none,lows = none,** kwargs)>
从文件或URL返回GeodataFrame。

参数
文件名: str,路径对象或类似文件的对象
要打开的文件或URL的绝对或相对路径,或任何具有read()方法的对象(例如Open File或Stringio)

bbox:元组| Geodataframe或Geoseries |多形的几何形状,默认没有
通过给定的边界框,地理系,地理框架或外形几何形状通过给定的边界框,滤波器特征。如果给出地理工具或地理框架,则可以解决CRS错误匹配。元组是(Minx,Miny,Maxx,Maxy),可以匹配成形几何对象的边界属性。不能与口罩一起使用。

蒙版: dict | Geodataframe或Geoseries |多形的几何形状,默认没有
与给定的dict样geojson几何形状,地层,地理框架或外形几何形状相交的特征过滤器。如果给出地理工具或地理框架,则可以解决CRS错误匹配。不能与bbox一起使用。

行: int或切片,默认无
通过传递整数(第一个n行)或slice()对象来加载特定行。

** kwargs
打开文件时,关键字arg arg将传递到fiona库中的打开或字节汇总方法。有关可能的关键字的更多信息,请输入:导入Fiona;帮助(fiona.open)

返回
geopandas.geodataframepandas.dataframe

如果ignore_geometry = true a pandas.dataframe将被返回。

GeoPandas read_file() has a rows option to limit the number of rows read (or to use a slice to read specific 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文