Python + GIS eGeopandas?

Li recentemente "A Dream of Red Mansions", e li que a Segunda Casa de Ning Rong foi ordenada pela Concubina Yuan para ir ao Templo Qingxu para lutar, e a cena onde havia multidões de pessoas e carros, tão imponente! Na Visão Qingxu, o Juiz Zhang pretende beijar Baoyu, mas Baoyu, que só tem Daiyu em seu coração, está naturalmente enojado. No segundo dia, Baoyu se sentiu muito desconfortável por causa das palavras de Zhang Taoist, e por causa da insolação de Daiyu, ele estava deprimido. Por esta razão, eu fui especialmente ao Pavilhão Xiaoxiang para ver Daiyu, mas eu não sabia que a visita era originalmente uma visita bem-intencionada, mas por causa do temperamento paranóico e voluntarioso de Daiyu, eles causaram um mal-entendido entre os dois. Até agora, os dois no Pavilhão Xiaoxiang no Grand View Garden estavam chorando e causando problemas, e até alarmaram a Mãe Jia. Embora os dois estivessem na mesma sala, eles estavam apaixonados por um coração. Um cobriu o rosto e chorou, e o outro suspirou no céu. Foi realmente preocupante assistir.

Não é de admirar que menino e menina se apaixonaram e não competiram entre si em tal relacionamento!

Como se diz:

Pensamentos emocionais entre sobrancelhas franzidas,

A figura de Ran Ran é lamentável.


Por muito tempo antes, eu achava que Python, como uma linguagem de tipagem fraca, era muito difícil de escrever, e a forma como era escrita sempre parecia estranha. Pode ser que eu esteja acostumado a escrever linguagens de tipagem forte ou que foi influenciado pelas idéias preconcebidas de Java ou Golang. Eu realmente não quero entrar em Python, sinto que a sintaxe é lenta e não "autêntica", embora seja muito poderosa e resolva problemas de forma acentuada, ainda me faz sentir que esta não é uma linguagem de programação que vale a pena aprender.

Até ler um post que resolvia um problema com apenas algumas linhas de código, parecia uma mudança repentina na minha opinião sobre o Python.

Na verdade, como ferramenta, não há diferença entre os prós e os contras de uma linguagem, seja ela uma linguagem estática ou dinâmica, tudo fica nos ombros do compilador e faz algum trabalho para resolver problemas de negócios. Como uma linguagem de alto nível, a sintaxe concisa do Python e as poderosas bibliotecas de terceiros são suas maiores vantagens, desde que possa nos trazer conveniência para resolver problemas, é uma boa ferramenta.

Vamos aprender juntos hoje, uma geopandasbiblioteca poderosa.

Frente

Como o uso de pip para instalar muitos servidores restritos de bibliotecas de terceiros está em países estrangeiros, a velocidade de download é muito lenta.

Portanto, para acelerar o download, podemos configurar a fonte global de download da imagem pip. Os passos são os seguintes:

1. Etapas de configuração usando Python:

  • Usando win+Eo Open File Manager, digite na barra de endereços %appdata%;
  • pipCrie uma nova pasta neste diretório ;
  • pip文件夹下,新建pip.ini文件;
  • pip.ini文件内,输入以下内容:

[global]

timeout=6000 index-url = mirrors.aliyun.com/pypi/simple

trusted-host = mirrors.aliyun.com

至此,我们可以流畅的使用pip安装任何第三方库了。

2. 使用Anaconda的配置步骤:

  • 进入Anaconda安装目录的Scripts目录;
  • 右键启动cmd命令行窗口,输入以下命令,并回车;

conda config --add channels mirrors.tuna.tsinghua.edu.cn/anaconda/pk… conda config --add channels mirrors.tuna.tsinghua.edu.cn/anaconda/pk… conda config --add channels mirrors.tuna.tsinghua.edu.cn/anaconda/cl… #设置搜索时显示通道地址 conda config --set show_channel_urls yes

3. 查看配置结果;

conda config --show channels

About GeoPandas

GeoPandas is an open source project to make working with geospatial data in python easier. GeoPandas extends the datatypes used by pandas to allow spatial operations on geometric types. Geometric operations are performed by shapely. Geopandas further depends on fiona for file access and matplotlib for plotting.

geopandas包详情:

顾名思义,GeoPandas 通过添加对地理空间数据的支持来扩展流行的数据科学库 pandas。

GeoPandas 中的核心数据结构是 geopandas.GeoDataFrame,它是pandas.DataFrame 的子类,可以存储几何列并执行空间操作。 geopandas.GeoSeriespandas.Series 的子类,用于处理几何图形。因此,您的 GeoDataFramepandas.Series 与传统数据(数字、布尔值、文本等)和 geopandas.GeoSeries 与几何(点、多边形等)的组合。

Install

如果你使用Anaconda,那么键入以下命令即可:

conda install geopandas
复制代码

如果你是使用Python,那么首先去下面的网站下载对应的依赖,因为pip不会帮助我们自己安装依赖。

www.lfd.uci.edu/~gohlke/pyt…

依赖包根据自己的python版本和OS选择合适的即可,包括

Fiona
pyproj
rtrre
shapely
GDAL
复制代码

实际按照上面的顺序安装会报错,因此实际的安装顺序如下:

GDAL->Fiona->pyproj->shapely->rtree
复制代码

安装命令为,以GDAL为例,其它亦然:

pip install GDAL-3.4.2-cp37-cp37m-win_amd64.whl
复制代码

验证一下是否安装成功:

Example

注:以下代码均可单独运行,并输出结果至控制台


geopandas.GeoSeries

构造POINT

from shapely.geometry import Point
from geopandas import geopandas

s = geopandas.GeoSeries([Point(1, 1), Point(2, 2), Point(3, 3)])
print(s)
复制代码

构造具有坐标系的POINT

from shapely.geometry import Point
from geopandas import geopandas

s = geopandas.GeoSeries(
    [Point(1, 1), Point(2, 2), Point(3, 3)], crs="EPSG:3857")
复制代码

geopandas.GeoSeries.area

1. 构造地理集合,并量算面积

from shapely.geometry import Polygon, LineString, Point
s = geopandas.GeoSeries(
    [
        Polygon([(0, 0), (1, 1), (0, 1)]),
        Polygon([(10, 0), (10, 5), (0, 0)]),
        Polygon([(0, 0), (2, 2), (2, 0)]),
        LineString([(0, 0), (1, 1), (0, 1)]),
        Point(0, 1)
    ]
)
area = s.area
print(area)
复制代码

geopandas.GeoSeries.boundary

构造地理集合,并返回低维对象(抽稀)

from shapely.geometry import Polygon, LineString, Point

from geopandas import geopandas
s = geopandas.GeoSeries(
    [
        Polygon([(108.923166,35.363084), (109.322899,35.274691), (109.355755,35.283365),(108.936301,35.380966),(108.923166,35.363084)]),
        LineString([(0, 0), (1, 1), (1, 0)]),
        Point(0, 0),
    ]
)
boundary = s.boundary
print(boundary)
复制代码

geopandas.GeoSeries.x

获取地理对象的X值

from shapely.geometry import Point
from geopandas import geopandas

s = geopandas.GeoSeries([Point(1, 1), Point(2, 2), Point(3, 3)])
s.x
print(s.x)
复制代码

geopandas.GeoSeries.within

返回两个几何图形是否包含,值为bool

from shapely.geometry import Polygon, LineString, Point
from geopandas import geopandas

s1 = geopandas.GeoSeries(
    [
        Polygon([(0, 0), (2, 2), (0, 2)]),
        Polygon([(0, 0), (1, 2), (0, 2)]),
        LineString([(0, 0), (0, 2)]),
        Point(0, 1),
    ],
)
s2 = geopandas.GeoSeries(
    [
        Polygon([(0, 0), (1, 1), (0, 1)]),
        LineString([(0, 0), (0, 2)]),
        LineString([(0, 0), (0, 1)]),
        Point(0, 1),
    ],
    index=range(1, 5),
)

polygon = Polygon([(0, 0), (2, 2), (0, 2)])
within = s1.within(polygon)

print(within)

print(s1.within(s2))

print(s1.within(polygon))
复制代码

geopandas.GeoSeries.to_json

地理集合转json

from shapely.geometry import Point
s = geopandas.GeoSeries([Point(1, 1), Point(2, 2), Point(3, 3)])
s.to_json()
复制代码

geopandas.GeoDataFrame.set_crs

GeoDataFrame设置CRS

import os
import sys
import pyproj
from shapely.geometry import Point
from geopandas import geopandas

# os.environ['PROJ_LIB'] = os.path.dirname(sys.argv[0])
# os.environ['PROJ_LIB'] = "D:\ProgramData\Anaconda3\Library\share\proj\"

d = {'col1': ['name1', 'name2'], 'geometry': [Point(1, 2), Point(2, 1)]}
gdf = geopandas.GeoDataFrame(d)
print(gdf.crs)
gdf = gdf.set_crs('epsg:4490')
print(gdf.crs)
复制代码

geopandas.read_file

读取矢量文件

from geopandas import geopandas
from geopandas.geodataframe import GeoDataFrame

df = geopandas.GeoDataFrame.from_file("W:\desktop\葛瑶\毕业论文\GIS\shp\城六区遗址公园面状.shp")

head = df.head(6)
print(head)
复制代码

Missing and empty geometries

缺省和空值

from shapely.geometry import Polygon

from geopandas import geopandas

s = geopandas.GeoSeries([Polygon([(0, 0), (1, 1), (0, 1)]), None, Polygon([])])

print(s.area)
print(s.union(Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])))
print(s.intersection(Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])))
复制代码

1. Creating a GeoDataFrame from a DataFrame with coordinates

import pandas as pd
import geopandas
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {'City': ['Buenos Aires', 'Brasilia', 'Santiago', 'Bogota', 'Caracas'],
     'Country': ['Argentina', 'Brazil', 'Chile', 'Colombia', 'Venezuela'],
     'Latitude': [-34.58, -15.78, -33.45, 4.60, 10.48],
     'Longitude': [-58.66, -47.91, -70.66, -74.08, -66.86]})

gdf = geopandas.GeoDataFrame(
    df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))

print(gdf.head())

world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

# We restrict to South America.
ax = world[world.continent == 'South America'].plot(
    color='white', edgecolor='black')

# We can now plot our ``GeoDataFrame``.
gdf.plot(ax=ax, color='red')

plt.show()
复制代码

运行结果如下:

2. create a Isodensity map

import pandas as pd
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
from shapely.geometry import Point, geo  # 经纬度转换为点
import adjustText as aT
import mapclassify
import warnings

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

map_name = "西安市区域专题图"

warnings.filterwarnings('ignore')
# 读取数据为geodataframe格式
geo_ = gpd.GeoDataFrame.from_file('W:\Google_Download\西安市.json', encoding='utf-8')
print(geo_)
geo_.head(3)
df = pd.read_csv(r'C:\Users\王斌\Desktop\POI.csv', encoding='utf-8')

df['geometry'] = list(zip(df['wgs84_lon'], df['wgs84_lat']))  # 经纬度组合为新列geometry,与形状里的该列对应
df['geometry'] = df['geometry'].apply(Point)  # 经纬度转化为坐标点
gpd_df = gpd.GeoDataFrame(df)

base = geo_.plot(color='lightblue', edgecolor='grey', figsize=(15, 15))  # 利用形状信息画底图
gpd_df.plot(ax=base, color='red', marker="o", markersize=50, alpha=0.01)  # 在底图上添加出租房位置
plt.gca().xaxis.set_major_locator(plt.NullLocator())  # 去掉x轴刻度
plt.gca().yaxis.set_major_locator(plt.NullLocator())  # 去掉y轴刻度

geo_.plot(figsize=(8, 6),  # 图像大小
          column='name',  # 分级设色字段
          # scheme='quantiles',  # MapClassify-分级类型
          legend=True,  # 图例
          cmap='Pastel1_r',  # 渐变色带的名称#Set2
          edgecolor='k')  # 边框颜色
# 图名
plt.Artist

plt.title('{}'.format(map_name), fontsize=18, fontweight='bold')

# 显示网格,透明度为50%
plt.grid(True, alpha=0.5)

plt.show()
#plt.savefig('./image/{}.png'.format(map_name), dpi=300)
复制代码

如下为运行结果,红色斑块为西安市城六区的POI点,为21万余点。

Summary

geopandas的函数较多,这里我选了几个运行和学习,大家有兴趣可以根据自己的需要进行学习。

geopandas作为一个Python与GIS结合的库,根据官网的介绍其提供的功能还是比较丰富的,能够进行较多的地理数据处理,也可以快速绘制一些基础地图,并能够灵活的对地图进行设计。

我感觉可以满足大部分的GIS小场景,但是遇到比较复杂的业务场景或者功能,可能还得借助桌面端的软件来辅助完成。

总体来讲,我感觉在解决一些小的GIS问题上来说还是很锋利的。

值得一学!

Error Summary

这里额外记录一个错误,我排查、上网找资料好久才算解决。

这里记录下来供大家参考!

错误的原因就是使用geopandas读取矢量文件或者绘图时会引发pyproj库的无效投影错误,错误截图如下:

pyproj.ecception.CRSError: Invalid projection:epsg:2263:(Internal Proj Error:···)

这是在Proj官网上给出的一个常见错误排查方案,能提供思路,但是没有解决问题。

Why am I getting the error “Cannot find proj.db”?

The file proj.db must be readable for the library to properly function. Like other resource files, it is located using a set of search paths. In most cases, the following paths are checked in order:

  • A path provided by the environment variable PROJ_LIB.
  • A path built into PROJ as its resource installation directory (typically ../share/proj relative to the PROJ library).
  • The current directory.

Note that if you’re using conda, activating an environment sets PROJ_LIB to a resource directory located in that environment.

最后在根据在网上查看许久发现线索指向Proj.db,该文件可能有问题,因为报错总是提示SQLite error,具体来说是关于初始化查询的一个错误,说明 Proj.db可能是有问题的。

使用如下代码打印出目前pyproj的资源指向路径:

data_dir = pyproj.datadir.get_data_dir()
print(data_dir)
复制代码

在我的笔记本上该路径指向D:\ProgramData\Anaconda3\Lib\site-packages\pyproj\proj_dir\share\proj

经查看该文件下的Proj.db为7.7M左右,但是pyproj官方提示的在Anaconda的安装目录/share/proj下的Proj.db大小为5.36M。

实在想不到解决办法的我,大胆地把D:\ProgramData\Anaconda3\Lib\site-packages\pyproj\proj_dir\share\proj下的Proj.db替换为了Anaconda的安装目录/share/proj下的Proj.db

正所谓:“无心插柳柳成荫”,没想到问题好像解决了。程序可以正确的进行矢量文件读取和制图。

按道理来讲,由Anaconda下载的依赖,其中的依赖关系会被较好的处理,很难出现这样的错误。

我记得我在网上好像看到了Pyproj库是建立在EPSG数据库之上的,这个错误可能是由与版本冲突造成的吧。

希望后面可以搞清楚原因。

知道其中缘由的同学,一定要私信我哈~

Acho que você gosta

Origin juejin.im/post/7079716000232374286
Recomendado
Clasificación