使用Visvalingam-Wyatt算法简化几何形状
项目描述
Visvalingam-Wyatt
Visvalingam-Wyatt线简化算法的Python实现。
此实现归功于Eliot Hallmark。此版本仅将其打包为Python模块。
使用
>>> import visvalingamwyatt as vw
>>> points = [(1, 2), (2, 3), (3, 4), ...]
>>> vw.simplify(points)
[(1, 2), (3, 4), ...]
点可以是任何类似Sequence
的对象,例如(list
,tuple
,公开__iter__
方法的自定义类)。
测试不同方法和阈值
simplifier = vw.Simplifier(points)
# Simplify by percentage of points to keep
simplifier.simplify(ratio=0.5)
# Simplify by giving number of points to keep
simplifier.simplify(number=1000)
# Simplify by giving an area threshold (in the units of the data)
simplifier.simplify(threshold=0.01)
处理地理数据的简写
import visvalingamwyatt as vw
feature = {
"properties": {"foo": "bar"},
"geometry": {
"type": "Polygon",
"coordinates": [...]
}
}
# returns a copy of the geometry, simplified (keeping 90% of points)
vw.simplify_geometry(feature['geometry'], ratio=0.90)
# returns a copy of the feature, simplified (using an area threshold)
vw.simplify_feature(feature, threshold=0.90)
命令行工具vwsimplify
可用于简化GeoJSON文件
# Simplify using a ratio of points
vwsimplify --ratio 0.90 in.geojson -o simple.geojson
# Simplify using the number of points to keep
vwsimplify --number 1000 in.geojson -o simple.geojson
# Simplify using a minimum area
vwsimplify --threshold 0.001 in.geojson -o simple.geojson
安装Fiona以简化任何地理数据层。
许可证
MIT