This commit is contained in:
Motiejus Jakštys 2020-03-30 16:42:53 +03:00
parent 5f1f20a16a
commit db18a17f66
2 changed files with 13 additions and 3 deletions

View File

@ -1,18 +1,22 @@
from collections import namedtuple from collections import namedtuple
from decimal import Decimal as Dec from decimal import Decimal as Dec
class Deg(namedtuple('Deg', ['deg', 'mm', 'ss'])): class Deg(namedtuple('Deg', ['deg', 'mm', 'ss'])):
def __str__(self): def __str__(self):
return "%03d-%02d-%.1f" % (self.deg, self.mm, self.ss) return "%03d-%02d-%.1f" % (self.deg, self.mm, self.ss)
def guess(inp): def guess(inp):
if isinstance(inp, str) and '-' in inp: if isinstance(inp, str) and '-' in inp:
deg, mm, ss = inp.split('-') deg, mm, ss = inp.split('-')
ddeg, dmm, dss = Dec(deg), Dec(mm), Dec(ss) ddeg, dmm, dss = Dec(deg), Dec(mm), Dec(ss)
return ddeg + dmm/60 + dss/3600 return ddeg + dmm/60 + dss/3600
else: else:
return Dec(instr) return Dec(inp)
def hms(deg): def hms(deg):
assert isinstance(deg, Dec) assert isinstance(deg, Dec)

View File

@ -5,6 +5,7 @@ import rtree
import os import os
from osgeo import ogr from osgeo import ogr
def main(): def main():
driver = ogr.GetDriverByName('gpkg') driver = ogr.GetDriverByName('gpkg')
@ -13,7 +14,11 @@ def main():
srs = inLayer.GetSpatialRef() srs = inLayer.GetSpatialRef()
outDataSource = driver.CreateDataSource(sys.argv[2]) outDataSource = driver.CreateDataSource(sys.argv[2])
outLayerName, _ = os.path.splitext(os.path.basename(sys.argv[2])) outLayerName, _ = os.path.splitext(os.path.basename(sys.argv[2]))
outLayer = outDataSource.CreateLayer(outLayerName, srs, geom_type=ogr.wkbLineString) outLayer = outDataSource.CreateLayer(
outLayerName,
srs,
geom_type=ogr.wkbLineString
)
points = extract_layer_points(inLayer) points = extract_layer_points(inLayer)
path = greedy_path(points) path = greedy_path(points)
@ -36,13 +41,14 @@ def extract_layer_points(layer):
points[i] = (p[0], p[1]) points[i] = (p[0], p[1])
return points return points
def greedy_path(points): def greedy_path(points):
"""Given a dict of points, return a "greedy" path between them. """Given a dict of points, return a "greedy" path between them.
It will start with a random point, and keep navigating to the closest one, It will start with a random point, and keep navigating to the closest one,
until all the points have been visited. until all the points have been visited.
""" """
idx = rtree.index.Index(interleaved = False) idx = rtree.index.Index(interleaved=False)
for (i, pt) in points.items(): for (i, pt) in points.items():
idx.insert(i, pt) idx.insert(i, pt)