Skip to content

Instantly share code, notes, and snippets.

@alanbernstein
Created February 10, 2021 05:11
Show Gist options
  • Save alanbernstein/54dae38370b0752eddcb1c387253480d to your computer and use it in GitHub Desktop.
Save alanbernstein/54dae38370b0752eddcb1c387253480d to your computer and use it in GitHub Desktop.
polyline smoothing attempts
[[55.60458963143672, 6.344134770404208], [55.6254632267828, 6.337129830206686], [55.60910039087562, 6.229813623881452], [55.62319315158302, 6.177102890074548], [55.599799681727454, 6.112288249833094], [55.6197243031257, 6.085758465395703], [55.57500185675112, 6.037293331710632], [55.57972586711283, 5.99042398847148], [55.531475137494226, 5.962734650863261], [55.460421247041275, 5.87334504507754], [55.43845503757297, 5.874789476632287], [55.38653276222971, 5.716568106677365], [55.34442623363374, 5.6649444409916905], [55.0906992233307, 5.521413353897024], [55.00428464574901, 5.444832031743397], [54.88500137356685, 5.403923196603118], [54.85919019379097, 5.373908570787792], [54.66938268686648, 5.3725558408613185], [54.58827630940152, 5.319732261307423], [54.52210828523082, 5.31060063116936], [54.45170456646131, 5.26900254435648], [54.43892241772447, 5.239180065187691], [54.430048050840036, 5.2568518437884455], [54.29608058774813, 5.205661074225492], [54.17762938393885, 5.122854327909668], [54.1250721254506, 5.053478803420626], [54.05583859008349, 5.067504809162117], [53.892297620796164, 5.181347190327159], [53.753959677102685, 5.177552149400188], [53.69261109097618, 5.2204457918475775], [53.68041801193546, 5.19270184677354], [53.70536845650908, 5.191683833290568], [53.667068597734676, 5.160118829659394], [53.67399197126227, 5.2120776093445675], [53.65286307327406, 5.228829145291736], [53.66437648062709, 5.257853911522921], [53.64022279614883, 5.299528168841551], [53.6678927865486, 5.3078680626907495], [53.671998921164096, 5.332316347813158], [53.65486940512473, 5.345290687996907], [53.60003863557006, 5.342254819925364], [53.599758342935516, 5.366713264198166], [53.65346720949631, 5.365591113231849], [53.65967250098817, 5.41093470939081], [53.58895442120837, 5.423325753283701], [53.61591249835013, 5.395873741726093], [53.59946611719199, 5.384471350041471], [53.570546354041895, 5.401940746868776], [53.452054649433315, 5.402673096419933], [53.396255599767194, 5.5410014201356645], [53.350345202001805, 5.5962058663119985], [53.321631377617145, 5.592037080982935], [53.307360601516194, 5.658635069225141], [53.26701930106281, 5.711142160640113], [53.26670668422784, 5.854651883514856], [53.32787342015466, 5.922110268488108], [53.334694133669814, 6.071364753433041], [53.34186000412809, 5.971317260749304], [53.34186887240988, 6.077748569487561], [53.35644240409859, 6.085795150567597], [53.3567327116339, 6.052601061583113], [53.385247968154516, 6.0866946733194744], [53.3769435858083, 6.1031517106972695], [53.34712077042703, 6.109585086365594], [53.33660323517172, 6.091396691557049], [53.28713046745968, 6.111748484707474], [53.249498336571534, 6.145740865246301], [53.30392404169611, 6.153279498369617], [53.33728631694488, 6.184005090244478], [53.36017401963582, 6.218549233500996], [53.371807491382185, 6.313112049660981], [53.25788105782836, 6.224039447231778], [53.162956083970556, 6.265764899371792], [53.20977771444897, 6.2989391834745], [53.24476499542698, 6.267182001648027], [53.30998086252666, 6.354002692871651], [53.40570799934681, 6.3877862752511545], [53.41615588100762, 6.428697942669817], [53.453786547116145, 6.435650956336649], [53.47233754834878, 6.516184578423177], [53.43579191500879, 6.463545693643505], [53.39766938887433, 6.468062353395542], [53.32759174020813, 6.426300005195301], [53.22237213361474, 6.437581136484591], [53.13785086083106, 6.412862309507949], [53.103135694797956, 6.3898928210291075], [53.12790851067221, 6.384793683839599], [53.10193713515687, 6.3578113674836665], [53.05581525129537, 6.46043507763624], [53.07745253587721, 6.472345916271409], [53.07609869081629, 6.443035937200436], [53.090087745252156, 6.432771122137838], [53.116416743605065, 6.518009969789489], [53.105606562525125, 6.587846489939209], [53.12908115638752, 6.649367487348493], [53.089449584251874, 6.681786430370995], [53.103819123213945, 6.743301289372765], [53.12272250566311, 6.729143967490855], [53.132660991937044, 6.763016973134466], [53.096265313599076, 6.778404006871901], [53.11690618711945, 6.788504860060221], [53.10658226739603, 6.845670379150897], [53.079274108346205, 6.844708709960225], [53.03549792973975, 6.91748448819745], [52.97378368352452, 6.945333030913079], [52.91156426858668, 7.076762337609859], [52.67748857714707, 7.1866445009213455], [52.462443881782036, 7.221232487158034], [52.330823451819136, 7.299497885830829], [52.1611991655593, 7.294090706443642], [52.10720026022236, 7.309924440547496], [51.99706793748025, 7.243387656704371], [51.922461103020844, 7.2454365062068025], [51.9176095803429, 7.273612247586979], [51.81258530101016, 7.301698912047742], [51.81291687451038, 7.333829880076909], [51.59564699725067, 7.427516158422444], [51.49024469944216, 7.397524295745101], [51.445437756009646, 7.418164212437491], [51.3494948744488, 7.392591822038324], [51.29389529908699, 7.399000353802128], [51.298447583070796, 7.381936892514632], [51.15902592687701, 7.460167274385299], [51.102516880484494, 7.518875630061095], [51.08714914674489, 7.580775752682476], [51.027573580371275, 7.639061443439701], [51.069275472478104, 7.736544116236039], [51.108798602112195, 7.703858920602164], [51.16542115095813, 7.73467031788542], [51.18701992544398, 7.818100385070015], [51.1936560507735, 7.825766082840651], [51.20713558623005, 7.832345756647035], [51.195188964508816, 7.7987698483148025], [51.25091633928239, 7.915304300074198], [51.25108186806792, 8.130081156010437], [51.308133752435275, 8.30950398955911], [51.28550082205042, 8.497304212901113], [51.3107075820727, 8.628327716508608], [51.27849406154491, 8.79759635525675], [51.29840335728433, 8.822498204564809], [51.30626088664416, 8.717845605862228], [51.34071736794413, 8.725432507672817], [51.32940940680638, 8.859372930932139], [51.3488667618681, 8.877725765287567], [51.361487988939174, 8.81546052115742], [51.34124836933062, 8.775226263972769], [51.37120772777556, 8.788305455170393], [51.369457044163426, 8.719362616777982], [51.397850274332804, 8.733253809513043], [51.42822730508556, 8.790622774481228], [51.40303397743685, 9.032929369824888], [51.459879818846765, 9.115809317756504], [51.50796065468706, 9.144449915553054], [51.542680522588526, 9.211329958015554], [51.542865211268406, 9.265574671871146], [51.635313577491274, 9.346784386341245], [51.6549120907558, 9.340192984376735], [51.61026696676591, 9.245233241152793], [51.64285219948965, 9.243251248538336], [51.713917080856554, 9.307863115440007], [51.833958323944415, 9.300870601363837], [52.04419619312922, 9.337454106615057], [52.163360774933594, 9.269992955201499], [52.244869085797326, 9.281416862691511], [52.325073566293355, 9.248690973576787], [52.379767473584515, 9.272286154813301], [52.43405772934741, 9.225362382426777], [52.640402319486434, 9.1913564451932], [52.727610792314344, 9.221540304895473], [52.809173900836825, 9.284497943507068], [52.897942464635186, 9.293125227611576], [52.9402986367825, 9.42158142515948], [53.04418515039497, 9.444459537934822], [53.089288095106426, 9.47856293685479], [53.10335193261341, 9.463123044333354], [53.08138090158232, 9.453163864091332], [53.083961539261175, 9.276366195183675], [53.12893661223561, 9.353809302972364], [53.13665850600503, 9.34956663107011], [53.14548787439004, 9.307636710059041], [53.152561514802215, 9.303118573744037], [53.15331643151761, 9.339145609086117], [53.17227789701814, 9.34607785648353], [53.14438520741473, 9.431210601980606], [53.183630263376536, 9.420028467504189], [53.18559999828567, 9.46409753654898], [53.20678679344596, 9.457292022916953], [53.20935606827755, 9.413707975361557], [53.22782166182436, 9.426028386423827], [53.248276765302776, 9.386057384794652], [53.32766407547582, 9.337993992185954], [53.27424064729387, 9.383598718241922], [53.351600554461456, 9.428436222415343], [53.308173144334, 9.444831096595262], [53.34339569200238, 9.491685003363376], [53.36736777379146, 9.476977520599483], [53.39000378030708, 9.503349260612996], [53.42771327669199, 9.43322678393981], [53.43379723036813, 9.468243002878074], [53.40889257921407, 9.477285913844272], [53.437816073499405, 9.490654776918738], [53.411372179872615, 9.506912623470106], [53.4539897196404, 9.50956105751483], [53.452309893475764, 9.481711146206273], [53.4615206335865, 9.498913988828017], [53.471726840557636, 9.471022892434057], [53.487084326576124, 9.46573457811439], [53.47982160219088, 9.538744028420844], [53.52091708080039, 9.553731615428244], [53.54956444955579, 9.531503444495057], [53.577855718254334, 9.57271804058167], [53.58739860428664, 9.557760189768643], [53.54993667513343, 9.51686964359938], [53.576752554025894, 9.540802947732843], [53.58040202936762, 9.495782939204261], [53.60642454265861, 9.502737922322119], [53.648953445228805, 9.545594084032398], [53.64884844232414, 9.582258069878687], [53.67039220399036, 9.562589917667347], [53.658154959534066, 9.521412184163127], [53.704704501147646, 9.540873068941247], [53.69619095664599, 9.498496348413633], [53.76421905917267, 9.54249142330719], [53.779805674703105, 9.493007886049238], [53.81693670471203, 9.472988504350992], [53.84768343763685, 9.319249906015251], [53.768241939604536, 9.227364243908234], [53.80344372530075, 9.255790213207213], [53.80732170462022, 9.220180806454682], [53.822129774311094, 9.270572815627078], [53.84757404987075, 9.281782219156867], [53.853946164197396, 9.253807568120816], [53.866063242110066, 9.290303718800988], [53.886946570441104, 9.284577692976477], [53.9415148977919, 9.224867695946408], [53.93121013748344, 9.180849889360925], [53.9628717865958, 9.222094425036154], [53.99500135372209, 9.140341933049905], [54.012395808824834, 9.190850887339842], [54.02852781506276, 9.175867431345686], [54.05307524959466, 9.1774261788402], [54.021422827208156, 9.19795277502367], [54.021907406308664, 9.222404413165274], [54.05134009134385, 9.220240536475494], [54.022602594972014, 9.235932409196863], [54.01972860505607, 9.276787818067941], [54.08995588941151, 9.293117900646939], [54.13996229586577, 9.347810096101734], [54.2076072041121, 9.339338028737455], [54.225416014385424, 9.40094038822765], [54.29609154051353, 9.414513114314923], [54.298626066759496, 9.393931842593185], [54.30971552215762, 9.387927661115219], [54.32107020278007, 9.435240994438718], [54.33966590408931, 9.379757741080391], [54.34621817013827, 9.411590064582613], [54.37880794704676, 9.406453029273772], [54.395926025798374, 9.434014031585988], [54.43992493753327, 9.378072591019738], [54.55223698666185, 9.322311783027141], [54.53985172423243, 9.297449982056376], [54.566686666793274, 9.322519361200555], [54.604517217263925, 9.300431851368346], [54.598730130481265, 9.32335745768162], [54.63857202470769, 9.367150929639214], [54.62984347746474, 9.406858824942187], [54.57701444195306, 9.432521955634565], [54.556465217218495, 9.48477770780364], [54.59863150482292, 9.492984411128155], [54.59671415448899, 9.441800693381404], [54.61725680587753, 9.478433075186127], [54.641620700847184, 9.464328787641644], [54.64914430721464, 9.388330512105586], [54.693668378170074, 9.39119522887121], [54.692733871995685, 9.320516868815586], [54.72664600141655, 9.276344560187136], [54.778260032195995, 9.26024437046367], [54.75967111619931, 9.250443411897244], [54.78584341399115, 9.234879310214955], [54.78556462879702, 9.194370016498603], [54.858939699153616, 9.157731327867406], [54.88750420868496, 9.08261053407614], [55.01611190337739, 9.095451914275884], [54.94891906518241, 9.06070111567509], [54.948811223254694, 9.043881830314678], [54.99820291647878, 9.042252654637794], [54.968002753912934, 8.995129707553131], [55.003347940173185, 8.981942990346168], [55.02738451662903, 8.998449043077938], [55.011728187726995, 9.02275174087123], [55.078599321865894, 9.021249244607707], [55.06019688604442, 8.967732775363089], [55.08256946218937, 8.973761358536565], [55.08985805199774, 8.93366450533762], [55.00531792526262, 8.903588408627801], [54.99448057980587, 8.919588590818933], [54.94792565387618, 8.8629311603978], [54.953657090316085, 8.896055746960574], [54.88310248028917, 8.902227149723418], [54.84478866151173, 8.867751748325045], [54.85132973512459, 8.833992459613185], [54.79576953265897, 8.792038465687666], [54.691559156476224, 8.764699623353469], [54.68311047477898, 8.733227435755834], [54.75054771443431, 8.51053576764807], [54.83128499539761, 8.391634746824062], [54.843215426594696, 8.300505082029092], [54.91863844036322, 8.212612158315201], [54.90744893121109, 8.139863501484182], [54.97170071357535, 8.025473210504364], [55.07447245746256, 8.000311775338034], [55.21240458074768, 8.075396790452793], [55.39338600643006, 8.220174366612673], [55.47683286484066, 8.373351689219481], [55.564433623429736, 8.433110414422405], [55.57793514566951, 8.47462303640215], [55.631475644175445, 8.493088329501497], [55.631713269506285, 8.45957219130615], [55.64704995242669, 8.524595220163498], [55.62275607994171, 8.529933529448332], [55.706011442081206, 8.575598715450125], [55.719200897601745, 8.55089644177501], [55.844460566196176, 8.679970218460829], [55.904502613838496, 8.684568070465494], [55.92098088688306, 8.664835674048224], [55.88834279974188, 8.648719295594647], [55.9082665453051, 8.649889099282214], [55.9092549705023, 8.608311385028776], [55.842844374093325, 8.50839186559244], [55.8683713808637, 8.460058523638066], [55.81931205962936, 8.427778895989443], [55.83527441306481, 8.401022920392279], [55.83351108458857, 8.36274835269845], [55.80245744435269, 8.340188941736344], [55.81756006775576, 8.323421835471512], [55.73312000186115, 8.215497779843131], [55.69837108590807, 8.11494621227126], [55.71796675961524, 8.086353152030687], [55.808209620032606, 8.080719268713397], [55.78892773060278, 8.020738509056045], [55.82516106851069, 7.919200078723872], [55.68758148730081, 7.736146209722732], [55.68744027613317, 7.646353212720993], [55.70583584842865, 7.635014010311549], [55.680603048181794, 7.621347397485794], [55.64494383735054, 7.530741370873888], [55.58096071338649, 7.476340131158467], [55.578936576158405, 7.429910071550527], [55.58824561858239, 7.4143837157266095], [55.54781127630072, 7.379256495493813], [55.59307925503799, 7.246190137780924], [55.62451379052726, 7.219578330270192], [55.63142173864854, 7.233997957143595], [55.61016577526448, 7.152458652913998], [55.63820812394857, 7.151882914640772], [55.65426194461302, 7.044343619944415], [55.67279930050929, 7.0567620908713025], [55.69072107812484, 7.00993475314152], [55.6787039002111, 6.961641085413521], [55.670671519572586, 6.983087249489504], [55.64320516840915, 6.973240670765174], [55.65483419559956, 6.876358951974174], [55.56265607690781, 6.68054557103494], [55.589270797225915, 6.679730942464178], [55.587436903708, 6.631698491402212], [55.603967558744415, 6.673876015894245], [55.63735847473545, 6.684850268994825], [55.652180962977596, 6.587746164226944], [55.681210518058435, 6.610412787247484], [55.66158177713481, 6.551442345135303], [55.59401897676755, 6.482914539429375], [55.591182598196944, 6.405217488687554], [55.60458963143672, 6.344134770404208], [55.60458963143672, 6.344134770404208]]
import json
from matplotlib import pyplot as plt
import numpy as np
from shapely.geometry import Point, Polygon, MultiPolygon
from descartes.patch import PolygonPatch
def main():
with open('border-sample-australia.json') as f:
xy = np.array(json.load(f))
xy_simplified = np.array(rdp(xy.tolist(), 0.05))
patch_buffered = dilate(xy, 1.0)
xy_buffered = patch_buffered.get_path().vertices
fig = plt.figure()
ax = fig.add_subplot(111)
plt.plot(xy[:,0], xy[:,1], 'r-', label='original')
plt.plot(xy_simplified[:,0], xy_simplified[:,1], 'b-', label='RDP')
plt.plot(xy_buffered[:,0], xy_buffered[:,1], 'g-', label="shapely buffer'd")
plt.legend()
plt.show()
def dilate(xy, r):
# https://shapely.readthedocs.io/en/latest/manual.html?highlight=buffer#object.buffer
poly = Polygon(xy)
poly_p = poly.buffer(r, join_style=1)
poly_pn = poly_p.buffer(-r, join_style=1)
patch = PolygonPatch(poly_pn)
return patch
def point_line_distance(z, p0, p1):
if p0 == p1:
return np.sqrt((z[0] - p0[0]) ** 2 + (z[1] - p0[1]) ** 2)
else:
n = abs((p1[0] - p0[0]) * (p0[1] - z[1]) - (p0[0] - z[0]) * (p1[1] - p0[1]))
d = np.sqrt((p1[0] - p0[0]) ** 2 + (p1[1] - p0[1]) ** 2)
return n / d
def rdp(points, epsilon):
"""Reduces a series of points to a simplified version that loses detail, but
maintains the general shape of the series.
https://github.com/sebleier/RDP
"""
dmax, mid = 0.0, 0
for i in range(1, len(points) - 1):
d = point_line_distance(points[i], points[0], points[-1])
if d > dmax:
dmax, mid = d, i
if dmax >= epsilon:
return rdp(points[:mid+1], epsilon)[:-1] + rdp(points[mid:], epsilon)
return [points[0], points[-1]]
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment