Commit 40515c87 authored by Kris Vanneste's avatar Kris Vanneste
Browse files

Simplified get_point_at_fraction_of_length method of LineData.

Added intersects, get_intersection and get_incremental_distance methods to LineData.
Added get_projected_distance and get_projected_point methods to LineData.
Added calc_distances_to and get_min_distance methods to LineData.
Renamed get_nearest_index_to_point method of LineData to get_nearest_index_to, and reimplemented it.
Added get_nearest_point_to method to LineData.

git-svn-id: https://svn.seismo.oma.be/svn/seismo/mapping/layeredbasemap/trunk@7597 40b490c5-b4d9-47cb-8714-9bef99b524d5
parent 26bd1313
......@@ -98,7 +98,7 @@ class LineData(SingleData):
def get_point_at_fraction_of_length(self, fraction):
assert 0 <= fraction <= 1
ls = self.to_shapely()
pt = ls.interpolate(ls.length * fraction)
pt = ls.interpolate(fraction, normalized=True)
return PointData(pt.x, pt.y)
def get_centroid(self):
......@@ -119,15 +119,149 @@ class LineData(SingleData):
label = self.label
return PointData(lon, lat, z=z, value=value, label=label, style_params=style_params)
def intersects(self, other):
"""
Determine if line intersects other feature (point, line or polygon)
:param other:
instance of :class:`PointData`, :class:`LineData`
or :class:`PolygonData`
:return:
bool
"""
return self.to_ogr_geom().Intersects(other.to_ogr_geom())
def get_intersection(self, line2):
pt = self.to_shapely().intersection(line2.to_shapely())
if pt:
return PointData.from_shapely(pt)
"""
Get point of intersection with other line
:param line2:
instance of :class:`LineData`
:return:
instance of :class:`PointData`
"""
#pt = self.to_shapely().intersection(line2.to_shapely())
#if pt:
# return PointData.from_shapely(pt)
intersection = self.to_ogr_geom().Intersection(line2.to_ogr_geom())
if intersection.GetPointCount():
lon, lat, z = intersection.GetPoint(0)
return PointData(lon, lat, z)
def get_intersection_distance(self, line2):
"""
Compute distance along line where intersection occurs with other line
:param line2:
instance of :class:`LineData`
def get_nearest_index_to_point(self, pt):
:return:
float, distance (in m)
"""
pt = self.get_intersection(line2)
return self.get_projected_distance(pt)
def get_projected_distance(self, pt):
"""
Compute distance to closest position (not necessarily a vertex)
along the line to a point (i.e. projection of point to line)
:param pt:
instance of :class:`PointData`
:return:
float, distance (in m)
"""
line = self.to_shapely()
pt = pt.to_shapely()
fraction = line.project(pt, normalized=True)
return self.get_length() * fraction
def get_projected_point(self, pt):
"""
Compute projection of point to line (i.e., position along line
corresponding to shortest distance to the point)
:param pt:
instance of :class:`PointData`
:return:
instance of :class:`PointData`
"""
line = self.to_shapely()
pt = pt.to_shapely()
fraction = line.project(pt, normalized=True)
pt = line.interpolate(fraction, normalized=True)
return PointData.from_shapely(pt)
def calc_distances_to(self, feature):
"""
Calculate distances to a feature
:param feature:
instance of :class:`PointData`, :class:`LineData` or :class:`PolygonData`
:return:
1-D or 2-D array, distances (in m)
"""
import mapping.geotools.geodetic as geodetic
distances = geodetic.spherical_distance(pt.lon, pt.lat, self.lons, self.lats)
return np.argmin(distances)
if isinstance(feature, PointData):
lons, lats = np.array([feature.lon]), np.array([feature.lat])
else:
lons, lats = np.asarray(feature.lons), np.asarray(feature.lats)
distances = geodetic.meshed_spherical_distance(np.asarray(self.lons),
np.asarray(self.lats), lons, lats)
if isinstance(feature, PointData):
distances = distances[0]
return distances
def get_min_distance(self, feature):
"""
Compute shortest distance to a feature
:param feature:
instance of :class:`PointData`, :class:`LineData` or :class:`PolygonData`
:return:
float, distance (in m)
"""
distances = self.calc_distances_to(feature)
return distances.min(axis=0)
def get_nearest_index_to(self, feature):
"""
Get index of closest point to a feature
:param feature:
instance of :class:`PointData`, :class:`LineData` or :class:`PolygonData`
:return:
int, point index
"""
distances = self.calc_distances_to(feature)
return np.argmin(distances, axis=0)
## Backwards compatibility
get_nearest_index_to_point = get_nearest_index_to
def get_nearest_point_to(self, feature):
"""
Get point on line that is closest to a feature
:param feature:
instance of :class:`PointData`, :class:`LineData` or :class:`PolygonData`
:return:
instance of :class:`PointData
"""
idx = self.get_nearest_index_to(feature)
return self.get_point_at_index(idx)
def to_polygon(self):
# TODO: should we check if first point == last point?
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment