标签: geopandas

在大型 GeoJSON 对象中查找多边形坐标交点的最有效方法

我正在开发一个需要坐标映射的项目 - 确定坐标点是否存在于一系列多边形中。映射的数量相当大 - 跨越 100 多个多边形的约 1000 万个坐标。

在继续之前,我已经查看了此处此处的问题。这个问题并不多余,因为它涉及动态点和静态多边形。

我通过在 200 万个多边形的子集中映射单个坐标来缩小该问题的项目范围。这是我使用的代码:

from shapely.geometry import shape, Point

f = open('path/to/file.geojson', 'r')
data = json.loads(f.read())

point = Point(42.3847, -71.127411)
for feature in data['features']:
    polygon = shape(feature['geometry'])
    if polygon.contains(point):
        print(polygon)
Run Code Online (Sandbox Code Playgroud)

迭代 200 万个多边形(在本例中为建筑足迹)大约需要 30 秒(时间太长)。

我也尝试过使用mplPath如下:

import matplotlib.path as mplPath

building_arrays = [np.array(data['features'][i]['geometry']['coordinates'][0])
                   for i, v in enumerate(tqdm(data['features']))]
bbPath_list = [mplPath.Path(building)
               for building in tqdm(building_arrays)]

for b in tqdm(bbPath_list):
    if b.contains_point((-71.1273842, 42.3847423)):
        print(b)
Run Code Online (Sandbox Code Playgroud)

这大约需要 6 秒。一个改进,但考虑到我需要的映射量,仍然有点慢。 …

python computational-geometry python-3.x shapely geopandas

1
推荐指数
1
解决办法
1067
查看次数

如何在GeoPandas中获取质心

Geopandas 的质心

我有两个位置,所以我想通过 python 从 geopandas 获取质心?我怎么做呢?

python geopandas jupyter-notebook

1
推荐指数
1
解决办法
5464
查看次数

使用 Cartopy 和 Geopandas 遮罩形状文件外部的区域

我有一组用 Cartopy 绘制的数据(经度、纬度、温度)。我可以给出的最小示例是下面的代码(只有 30 个数据点)

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import matplotlib.colors as clr
import pandas as pd
import numpy as np

from metpy.interpolate import interpolate_to_grid, remove_nan_observations
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature

canada_east = -95
canada_west = -101.8
canada_north = 52.8
canada_south = 48.85

central_lon = (canada_east + canada_west)/2
central_lat = (canada_north + canada_south)/2

crs = ccrs.LambertConformal(central_longitude = central_lon, central_latitude = central_lat)
lat = np.array([49.8134 50.904  50.698  49.095  49.436  49.9607 …
Run Code Online (Sandbox Code Playgroud)

python colors shapefile cartopy geopandas

1
推荐指数
1
解决办法
1458
查看次数

在Python 3.6中从点(gpd.geodataframe对象)创建栅格

我想使用对象从点文件创建光栅文件 (.tif)geopandas.geodataframe.GeoDataFrame

我的数据框有两列:[几何] 和 [值]。目标是使用[Value]值在[geometry]点制作10m 分辨率的栅格

我的数据集是:

geometry |  Value
0  | POINT (520595.000 5720335.000) | 536.678345
1  | POINT (520605.000 5720335.000) | 637.052185
2  | POINT (520615.000 5720335.000) | 1230.553955
3  | POINT (520625.000 5720335.000) | 944.970642
4  | POINT (520635.000 5720335.000) | 1094.613281
5  | POINT (520645.000 5720335.000) | 1123.185181
6  | POINT (520655.000 5720335.000) | 849.37634
7  | POINT (520665.000 5720335.000) | 1333.459839
8  | POINT (520675.000 5720335.000) …
Run Code Online (Sandbox Code Playgroud)

python raster geopandas rasterio

1
推荐指数
1
解决办法
1188
查看次数

如何从 GeoJSON 创建要素集合

我有一个Feature CollectionPolygonsMultiPolygons必须首先将其写入临时文件中,然后使用 geopandas.GeoDataFrame.from_file(tmp_json_file) 加载它,我正在寻找一种无需临时文件即可完成此操作的方法。我尝试过使用geopandas.GeoDataFrame.from_feature(),它对于简单多边形的特征集合工作得很好,但我不能让它适用于Feature CollectionPolygonsMultiPolygons我正在考虑做类似下面的事情,但它还没有工作。

features_collection = []

for feature in json_data['features']:
   tmp_properties = {'id': feature['properties']['id']}

   if is_multipolygon (feature):
       tmp = Feature(geometry=MultiPolygon((feature['geometry']['coordinates'])), properties=tmp_properties)
   else: 
       Feature(geometry=Polygon((feature['geometry']['coordinates'])), properties=tmp_properties)
   features_collection.append(tmp)

collection = FeatureCollection(features_collection)

return geopandas.GeoDataFrame.from_features(collection['features'])
Run Code Online (Sandbox Code Playgroud)

GeoJSON 取自 API,返回领土(某些领土由单个多边形建模,其他由一组多边形(格式为 MultiPolygon)建模。

GeoJSON 的结构如下: http: //pastebin.com/PPdMUGkY

我从上面的函数中收到以下错误:

Traceback (most recent call last):
  File "overlap.py", line 210, in <module>
    print bdv_json_to_geodf(contours_bdv)
  File "overlap.py", line 148, in json_to_geodf
    return geopandas.GeoDataFrame.from_features(collection['features'])
  File "/Library/Python/2.7/site-packages/geopandas/geodataframe.py", line 179, …
Run Code Online (Sandbox Code Playgroud)

gis geojson geopandas

0
推荐指数
1
解决办法
2748
查看次数

如何为Geopandas图制作表格图例

我正在使用geopandas绘制一个Choropleth地图,我需要绘制一个定制的表格图例。这个问题的答案显示了如何获取轮廓图的表格图例。我在下面的代码中使用它:

import pandas as pd
import pysal as ps
import geopandas as gp
import numpy as np
import matplotlib.pyplot as plt

pth = 'outcom.shp'
tracts = gp.GeoDataFrame.from_file(pth)
ax = tracts.plot(column='Density', scheme='QUANTILES')

valeur = np.array([.1,.45,.7])
text=[["Faible","Ng<1,5" ],["Moyenne","1,5<Ng<2,5"],[u"Elevee", "Ng>2,5"]]
colLabels = ["Exposition", u"Densite"]
tab = ax.table(cellText=text, colLabels=colLabels, colWidths = [0.2,0.2],    loc='lower right', cellColours=plt.cm.hot_r(np.c_[valeur,valeur]))
plt.show()
Run Code Online (Sandbox Code Playgroud)

这是我得到的结果: 在此处输入图片说明

因此,基本上,如您所见,地图和表格中的类颜色之间没有任何联系。我需要具有地图上所示表格中的确切颜色。图例中显示的“ NG值”应从我正在绘制的“ DENSITY”列中提取。

但是,由于我没有等高线图可从中提取颜色图,因此我不知道如何链接表格图例和地图的颜色。

matplotlib python-2.7 geopandas

0
推荐指数
1
解决办法
1379
查看次数

GeoPandas 将纬度/经度点重新投影到 UTM 给出 Inf、Inf

使用 geopandas 时,尝试将纬度/经度中的一些点转换为 UTM。但是,转换给了我 Point(inf inf)...我的代码有什么问题?有人可以向我指出问题吗。Thks

GPS =  pd.read_excel('coordinates.xls', sheet_name='GPS', usecols = "A,B,F,G", header = 0)
GPS.columns = ['Date', 'Time', 'Lat', 'Lon']

GPS_LatLon = GPS[["Lat","Lon"]]
geodf = gpd.GeoDataFrame(GPS_LatLon,
                   crs={'init': 'epsg:4326'},
                   geometry=GPS_LatLon.apply(lambda row: shapely.geometry.Point((row.Lat, row.Lon)), axis=1))

geodf = geodf.to_crs({'init': 'epsg:32633'})

geodf["x"] = geodf.geometry.apply(lambda row:row.x)
geodf["y"] = geodf.geometry.apply(lambda row:row.y)

GPS_UTM = pd.DataFrame(geodf)
GPS_UTM.to_excel("GPS_utm.xls")
Run Code Online (Sandbox Code Playgroud)

这是 GeoDataFrame 步骤之后的我的数据

        Lat         Lon               geometry
0     1.416176  13.869467  POINT (1.416 13.869)
1     1.416176  13.869466  POINT (1.416 13.869)
2     1.416176  13.869465  POINT (1.416 13.869)
3     1.416177  13.869465  POINT (1.416 …
Run Code Online (Sandbox Code Playgroud)

python pandas geopandas

0
推荐指数
1
解决办法
1718
查看次数

在 Geopandas 中过滤和合并特定距离内的两个数据帧中的点

对于两个 GeoPandas 数据框如下:

df1:

     id  sMiddleLng  sMiddleLat  p1_sum  p2_sum  \
0  325782  109.255034   34.691754     0.0     0.0   
1   84867  107.957177   33.958289     0.0     0.0   
2   13101  107.835338   33.739493     0.0     0.0   
3   92771  109.464280   33.980666     0.0     0.0   
4   86609  108.253830   33.963262     0.0     0.0   

                            geometry  
0  POINT (109.255033915 34.69175367)  
1  POINT (107.957177305 33.95828929)  
2    POINT (107.8353377 33.73949313)  
3   POINT (109.46428019 33.98066616)  
4  POINT (108.253830245 33.96326193)  
Run Code Online (Sandbox Code Playgroud)

df2:

     fnid  sMiddleLng  sMiddleLat  p1_sum  p2_sum  \
0  361104  102.677887   36.686408     0.0     0.0   
1  276307  103.268356   36.425372     0.0     0.0 …
Run Code Online (Sandbox Code Playgroud)

python geometry pandas geopandas

0
推荐指数
1
解决办法
1452
查看次数

为什么 geopandas 中的线图在显示分层时忽略绘图顺序

作为我正在进行的社区项目的一部分,我一直在尝试使用 geopandas 和从 openstreetmaps 导出的一些数据。为了试验数据可视化方面,我导出了城镇的一部分,并使用 geopandas 绘制了它。这很有效,除了无论我绘制图层的顺序如何,道路图层都会出现在其他所有图层之上。

我尝试用谷歌搜索一个解决方案,一切都表明您执行绘图的顺序应该决定分层,最后要绘制的东西出现在其他所有东西之上。应用这种逻辑,道路线应该位于建筑物和欺骗的 GPS 定位点下方?有人可以告诉我如何解决这个问题,以便我可以控制哪一层在顶部吗?

在此输入图像描述

import matplotlib.pyplot as plt
import geopandas as gpd
import numpy as np

# import and clean up map data
derby_roads = gpd.read_file('map/roads.geojson')
derby_roads_clean = derby_roads[derby_roads.highway.str.contains('motorway|trunk|primary|secondary|tertiary|residential')]
derby_buildings = gpd.read_file('map/buildings.geojson')
#print(derby_roads_clean)
#print(derby_buildings.head())

# generate random gps data
# min x -1.4879, max x -1.4407
# min y 52.8801, max y 52.8962
points_x = np.random.uniform(-1.4879, -1.4407, size = (50,))
points_y = np.random.uniform(52.8801, 52.8962, size = (50,))
points_z = np.random.uniform(0, 100, size = (50,)) …
Run Code Online (Sandbox Code Playgroud)

python matplotlib geopandas

0
推荐指数
1
解决办法
763
查看次数

向 GeoDataFrame 添加新列,当它匹配时,它说值的长度与索引不匹配

目标是读取 shapefile,获取每个多边形(城市)的面积,并将该面积添加到名为“面积”的新列中。

cs = gpd.read_file('munis.shp')
area_fd = []
area = cs['geometry'].map(lambda p: p.area / 10**6)
area_fd.append(area)
cs = cs.assign(Area =  area_fd) #or cs['Area'] = area_fd
Run Code Online (Sandbox Code Playgroud)

当我运行时,print(cs['NAME_2'].head)它返回 38 munis (0-38) 并print(area_fd)给出 38 个值。但是当我运行代码时,它说ValueError: Length of values does not match length of index。我已经查找了类似的 GISSE 和 SO 线程,但没有解决方案有效。

indexing shapefile python-3.x pandas geopandas

0
推荐指数
1
解决办法
3865
查看次数

仅将每个多线串转换为一个线串

在此shapefile中,几何列是线串,除了 4 个流到达 ( 8168547, 8171738, 8170616 ,8169920) 是多线串之外。

在此输入图像描述

我需要将每个多线串仅转换为一个线串。我尝试了很多方法但没有成功。例如,我st_cast在 R 中尝试了 sf 包。但是,它增加了行数(它将每个多线串转换为多个线串)。

如何将每个多线串仅转换为一个线串?

multilinestring geopandas r-sf

0
推荐指数
1
解决办法
7676
查看次数

如何加快大数据创建Point GeoSeries的速度?

我有两个一维数组,想将它们组合成一个 Point GeoSeries,如下所示:

import numpy as np
from geopandas import GeoSeries
from shapely.geometry import Point

x = np.random.rand(int(1e6))
y = np.random.rand(int(1e6))
GeoSeries(map(Point, zip(x, y)))
Run Code Online (Sandbox Code Playgroud)

在我的笔记本电脑上大约需要 5 秒。是否可以加速GeoSeries的生成?

python pandas shapely geopandas

0
推荐指数
1
解决办法
48
查看次数