天天看点

基于shapefile的道格拉斯普克算法(python)

文章目录

  • ​​前言​​
  • ​​算法的基本思路​​
  • ​​代码V1​​
  • ​​代码v2​​
  • ​​参考​​

前言

Douglas-Peukcer算法由D.Douglas和T.Peueker于1973年提出,是线状要素抽稀的经典算法。用它处理大量冗余的几何数据点,既可以达到数据量精简的目的,又可以在很大程度上保留几何形状的骨架。

算法的基本思路

将待处理曲线的首末点虚连一条直线,求所有中间点与直线的距离,并找出最大距离值dmax ,用dmax与抽稀阈值threshold相比较:

若dmax < threshold,这条曲线上的中间点全部舍去;

若dmax ≥ threshold,则以该点为界,把曲线分为两部分,对这两部分曲线重复上述过程,直至所有的点都被处理完成。

基于shapefile的道格拉斯普克算法(python)

代码V1

import geopandas as gpd
from geopandas._vectorized import simplify
from shapely.geometry import Polygon, mapping

def simplify_shp(in_shp, out_shp, tolerance):
    """
    :param in_shp: the path of input shapefile
    :param out_shp: the path of output shapefile
    :return:
    """
    gdf = gpd.read_file(in_shp) #LINESTRING
    gdf['geometry'] = simplify( gdf['geometry'], tolerance=tolerance)
    gdf.to_file(out_shp, driver="ESRI Shapefile")      

下图红色线为简化后的版本

基于shapefile的道格拉斯普克算法(python)

代码v2

from posixpath import basename
import geopandas as gpd
from geopandas._vectorized import simplify
from pip import main
from shapely.geometry import Polygon, mapping
import os
import topojson as tp


def simplify_shp2(in_shp, out_shp, tolerance=0.0001):
    """
    :param in_shp: the path of input shapefile
    :param out_shp: the path of output shapefile
    :return:
    """
    gdf = gpd.read_file(in_shp) #LINESTRING
    topo = tp.Topology(gdf, prequantize=False)
    gdf = topo.toposimplify(tolerance).to_gdf()
    gdf.to_file(out_shp, driver="ESRI Shapefile")      

参考