Skip to content

Instantly share code, notes, and snippets.

@atlefren
Created February 6, 2017 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atlefren/f5a170ad01794ae103d4d0c09f96d115 to your computer and use it in GitHub Desktop.
Save atlefren/f5a170ad01794ae103d4d0c09f96d115 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# -*- coding: utf-8 -*-
import json
from itertools import izip
from vincenty import vincenty
from shapely.geometry import shape
def line_length(line):
# our line is in EPSG:4326, shapely uses euacledian distance for .lengtg
# so we have to use thg vincenty formula
coords = line.coords
return sum([vincenty((a[1], a[0]), (b[1], b[0]))
for a, b in izip(coords, coords[1:])]) * 1000
def open_file(filename):
with open(filename, 'r') as f:
return f.read()
def read_geoms(filename):
data = json.loads(open_file(filename))
geoms = []
for feature in data['features']:
geoms.append(shape(feature['geometry']))
return geoms
if __name__ == '__main__':
line = read_geoms('line.geojson')[0]
length = line_length(line)
print 'line length: %s m' % length
points = read_geoms('points.geojson')
for point in points:
print 'computing point ', point
snapped = line.interpolate(line.project(point))
fraction = line.project(point, normalized=True)
print 'snapped to ', snapped
from_start = length * fraction
from_end = length - from_start
print 'distance from start: %s m' % from_start
print 'distance from end : %s m' % from_end
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment