本文完整代码、数据集下载、在线运行可以访问这个链接:时空数据Python常用包案例 配套习题与答案可以访问这个链接:时空数据Python常用包案例 - 实操练习题(附答案)
Python处理时空数据会用到如下常用库:
库
作用
GeoPands
geopandas是建立在GEOS、GDAL、PROJ等开源地理空间计算相关框架之上的,类似pandas语法风格的空间数据分析Python库,其目标是尽可能地简化Python中的地理空间数据处理,减少对Arcgis、PostGIS等工具的依赖,使得处理地理空间数据变得更加高效简洁,打造纯Python式的空间数据处理工作流。
Shapely
shapely是一个BSD授权的Python包。是专门做图形计算,用于操作和分析笛卡尔坐标系中的几何对象 ,基本上图形线段,点的判断包里都有,shapely里主要由Point,LineString,Polygon这三类组成。
pyproj
pyproj可以方便地进行坐标转换,包含了地理坐标系、坐标系、ECEF、BLH、ECI,ENU等坐标系,简单易用
Fiona
Fiona可以读和写地理数据文件,从而帮助Python程序员将地理信息系统与其他计算机系统结合起来。Fiona包含连接地理空间数据抽象库(GDAL)的扩展模块。它专注于以标准的Python IO风格读写数据,并依赖于熟悉的Python类型和协议,如文件、字典、映射和迭代器,而不是GDAL的OpenGIS参考实现(OGR)的特定类。
rasterio
Rasterio使用更少的非惯用扩展类和更多的惯用python类型和协议表达gdal的数据模型,同时执行与gdal的python绑定一样快
以下分别罗列这些库的简单示例:
GeoPands
% matplotlib inline
import numpy as np
import matplotlib. pyplot as plt
from geopandas import GeoSeries, GeoDataFrame, read_file
from shapely. geometry import Point
from pandas import Series
下载纽约市各区的边界数据:Bytes of the Big Apple
boros = read_file( '/home/mw/input/st201516189/nybb.shp' )
boros. set_index( 'BoroCode' , inplace= True )
boros. sort_index( inplace= True )
boros
BoroName
Shape_Leng
Shape_Area
geometry
BoroCode
1
Manhattan
358408.460709
6.364467e+08
MULTIPOLYGON (((981219.056 188655.316, 980940....
2
Bronx
464400.198868
1.186973e+09
MULTIPOLYGON (((1012821.806 229228.265, 101278...
3
Brooklyn
741185.900596
1.937597e+09
MULTIPOLYGON (((1021176.479 151374.797, 102100...
4
Queens
897040.298576
3.045168e+09
MULTIPOLYGON (((1029606.077 156073.814, 102957...
5
Staten Island
330466.075042
1.623827e+09
MULTIPOLYGON (((970217.022 145643.332, 970227....
boros. reset_index( inplace= True )
boros. set_index( 'BoroName' , inplace= True )
boros
BoroCode
Shape_Leng
Shape_Area
geometry
BoroName
Manhattan
1
358408.460709
6.364467e+08
MULTIPOLYGON (((981219.056 188655.316, 980940....
Bronx
2
464400.198868
1.186973e+09
MULTIPOLYGON (((1012821.806 229228.265, 101278...
Brooklyn
3
741185.900596
1.937597e+09
MULTIPOLYGON (((1021176.479 151374.797, 102100...
Queens
4
897040.298576
3.045168e+09
MULTIPOLYGON (((1029606.077 156073.814, 102957...
Staten Island
5
330466.075042
1.623827e+09
MULTIPOLYGON (((970217.022 145643.332, 970227....
plt. figure( figsize= ( 8 , 8 ) )
boros. plot( )
plt. figure( figsize= ( 8 , 8 ) )
boros. plot( alpha= 0.0 )
boros. geometry. convex_hull. plot( )
plt. figure( figsize= ( 8 , 8 ) )
eroded = boros. geometry. buffer ( - 5280 )
boros. plot( alpha= 0.0 )
eroded. plot( )
eroded. area
BoroName
Manhattan 1.128785e+07
Bronx 3.371876e+08
Brooklyn 6.711072e+08
Queens 1.301421e+09
Staten Island 7.263977e+08
dtype: float64
boros. geometry. area
BoroName
Manhattan 6.364464e+08
Bronx 1.186974e+09
Brooklyn 1.937596e+09
Queens 3.045168e+09
Staten Island 1.623829e+09
dtype: float64
inland = 100.0 * eroded. area / boros. geometry. area
boros[ 'inland_fraction' ] = inland
boros
BoroCode
Shape_Leng
Shape_Area
geometry
inland_fraction
BoroName
Manhattan
1
358408.460709
6.364467e+08
MULTIPOLYGON (((981219.056 188655.316, 980940....
1.773574
Bronx
2
464400.198868
1.186973e+09
MULTIPOLYGON (((1012821.806 229228.265, 101278...
28.407326
Brooklyn
3
741185.900596
1.937597e+09
MULTIPOLYGON (((1021176.479 151374.797, 102100...
34.636066
Queens
4
897040.298576
3.045168e+09
MULTIPOLYGON (((1029606.077 156073.814, 102957...
42.737251
Staten Island
5
330466.075042
1.623827e+09
MULTIPOLYGON (((970217.022 145643.332, 970227....
44.733620
让我们创建一个正常的pandasSeries
,其中包含2010年人口普查中每个区的人口值。
population = Series( {
'Manhattan' : 1585873 , 'Bronx' : 1385108 , 'Brooklyn' : 2504700 ,
'Queens' : 2230722 , 'Staten Island' : 468730 } )
population
Manhattan 1585873
Bronx 1385108
Brooklyn 2504700
Queens 2230722
Staten Island 468730
dtype: int64
boros[ 'population' ] = population
boros
BoroCode
Shape_Leng
Shape_Area
geometry
inland_fraction
population
BoroName
Manhattan
1
358408.460709
6.364467e+08
MULTIPOLYGON (((981219.056 188655.316, 980940....
1.773574
1585873
Bronx
2
464400.198868
1.186973e+09
MULTIPOLYGON (((1012821.806 229228.265, 101278...
28.407326
1385108
Brooklyn
3
741185.900596
1.937597e+09
MULTIPOLYGON (((1021176.479 151374.797, 102100...
34.636066
2504700
Queens
4
897040.298576
3.045168e+09
MULTIPOLYGON (((1029606.077 156073.814, 102957...
42.737251
2230722
Staten Island
5
330466.075042
1.623827e+09
MULTIPOLYGON (((970217.022 145643.332, 970227....
44.733620
468730
boros[ 'pop_density' ] = boros[ 'population' ] / boros. geometry. area * 5280 ** 2
boros. sort_values( 'pop_density' , ascending= False )
The history saving thread hit an unexpected error (OperationalError('attempt to write a readonly database')).History will not be written to the database.
BoroCode
Shape_Leng
Shape_Area
geometry</