【问题标题】:Geopandas python empty geometries / missing valuesGeopandas python空几何/缺失值
【发布时间】:2020-05-07 14:21:59
【问题描述】:

我正在使用 geopandas 运行 python 代码来分析数据。我得到一个错误:问题与最后 5 行有关 这是我使用的数据https://www.fr.freelancer.com/users/l.php?url=https:%2F%2Fapp.box.com%2Fshared%2Fstatic%2Fyig4yill3xl83n7361c5q88kehikhje0.zip&sig=ec739bd7319b7823aa4c1d776a8d5ffc3c2da5040abfa799549180c82f1f93bd
这是代码

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import geopandas as gpd
from descartes.patch import PolygonPatch
from mpl_toolkits.axes_grid1 import make_axes_locatable

svi = gpd.read_file(r'C:\Users\Hamza\Desktop\Freelancer\SVI_IL2018.shp') # use a Tab key after you type data/ to select a file in the folder
svi.head() # show SVI by tracts in Ilinois

type(svi)
type(svi['geometry'])
svi['geometry'].geom_type
svi['geometry'].area
svi.crs
svi2 = svi.to_crs('epsg:3723')  # change CRS to UTM 16N
svi.plot()  # plot a GeoDataFrame. This shows tracts in Illinois
svi2 = svi.to_crs('epsg:3723')  # change CRS to UTM 16N
svi2.plot()
svi2.head()
ilcounty = svi2.dissolve(by='STCNTY')  # dissolve tracts by county
# map overall percentile ranking of social vulnerability
svi2.plot(column='RPL_THEMES', figsize=(6, 8), legend=True)  # figsize = (width, height) in inches, default (6.4, 4.8)
svi2['RPL_THEMES'].describe()  # get descriptive statistics of RPL_THEMES
svi2['RPL_THEMES'].hist()  # show histogram of RPL_THEMES
svi3 = svi2.replace(-999, np.nan)  # replace -999 with NaN (null values)
svi3['RPL_THEMES'].describe()
svi3.plot(column='RPL_THEMES', figsize=(8, 10), legend=True)
svi3.plot(column='RPL_THEMES', figsize=(8, 10), legend=True, cmap='YlOrBr')
svi3.plot(column='RPL_THEMES', figsize=(8, 10), legend=True, cmap='hot_r')
tri = gpd.read_file(r'C:\Users\Hamza\Desktop\Freelancer\TRI.geojson')
tri.crs
schools = gpd.read_file(r'C:\Users\Hamza\Desktop\Freelancer\schools.shp')
schools.head()
# 3) show the CRS of a GeoDataFrame schools. They should be in SPCS83
schools.crs
type(schools)
type(schools['geometry'])
tri.plot()
schools.plot()
svi3.plot()
# create fig (figure object) and ax (axis object)
fig, ax = plt.subplots(figsize=(8, 10))
# by default this creates a figure with one subplot (axis) as parameters for nrows and ncols are omitted
# if you create multiple axes, it would be good to name axes instead of ax

# plot three GeoDataFrames on the ax object created above
svi3.plot(ax=ax, column='RPL_THEMES')  # plot 'RPL_THEMES' on ax
tri.plot(ax=ax, color='r')  # plot tri on ax in red
schools.plot(ax=ax, color='g')  # plot schools on ax in green
svi4 = svi3.to_crs('epsg:3435')  # change CRS to UTM 16N
svi4.crs
# show three GeoDataFrames in one figure
fig, ax = plt.subplots(figsize=(8, 10))

svi4.plot(ax=ax, column='RPL_THEMES')  # map 'RPL_THEMES'
tri.plot(ax=ax, color='r')  # show tri in red
schools.plot(ax=ax, color='g')  # show schools in green
path = 'https://data.cityofchicago.org/api/geospatial/cauq-8yn6?method=export&format=GeoJSON'
comm = gpd.read_file(path)
comm.head()
comm.crs
comm = comm.to_crs('epsg:3435')  # change WGS84 to SPC IL East
comm.crs
# clip svi4 by comm
svi5 = gpd.clip(svi4, comm)

# clip tri by comm
tri2 = gpd.clip(tri, comm)
# warning of change in functionality. you can ignore this.

这是错误 C:\Users\Hamza\AppData\Local\Programs\Python\Python37\lib\site-packages\geopandas\geoseries.py:358: UserWarning: GeoSeries.notna() 先前为缺失(无)和空几何返回 False .现在,它只为缺失值返回 False。由于调用 GeoSeries 包含空几何图形,因此与以前版本的 GeoPandas 相比,结果发生了变化。 给定一个 GeoSeries 's',您可以使用 '~s.is_empty & s.notna()' 来恢复旧的行为。

要进一步忽略此警告,您可以执行以下操作: 进口警告; warnings.filterwarnings('ignore', 'GeoSeries.notna', UserWarning) 返回 self.notna()

【问题讨论】:

    标签: python geopandas


    【解决方案1】:

    这不是错误,而是 GeoPandas 现在在更改以前版本之一中的行为后显示警告。如果问题出在最后几行,很可能是由clip 引起的,所以应该不是问题。在这种情况下,GeoPandas 应该使警告静音。你可以愉快地忽略它。

    【讨论】:

    • 我怎么能忽略它们,我的意思是,不让它们出现在我的代码中
    • 那是字面上写在警告本身:)。 To further ignore this warning, you can do: import warnings; warnings.filterwarnings('ignore', 'GeoSeries.notna', UserWarning)
    猜你喜欢
    • 2020-01-20
    • 2021-02-23
    • 1970-01-01
    • 2019-10-08
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 2019-09-24
    相关资源
    最近更新 更多