def get_convex_hull_coordinates(gtfs):
"""
Parameters
----------
gtfs: gtfs.GTFS
Returns
-------
lons: list
of floats
lats: list
of floats
"""
lons, lats = _get_stop_lat_lons(gtfs)
lon_lats = list(zip(lons, lats))
polygon = MultiPoint(lon_lats).convex_hull
hull_lons, hull_lats= polygon.exterior.coords.xy
return hull_lats, hull_lons
python类MultiPoint()的实例源码
def compute_buffered_area_of_stops(lats, lons, buffer_meters, resolution=16):
# geo_series = gp.GeoSeries([Point(lon, lat) for lon, lat in zip(lons, lats)])
# geo_series.crs = {'init' :'epsg:4326'}
# geo_series = geo_series.to_crs({'init':'epsg:3857'})
# circles = geo_series.buffer(buffer_meters, resolution=resolution)
# multi_points = circles.unary_union
# return multi_points.area
if len(lons) > 1:
lon_meters, lat_meters = _get_lon_lat_meters(lons, lats)
else:
lon_meters = lons
lat_meters = lats
return MultiPoint(points=list(zip(lon_meters, lat_meters))).buffer(buffer_meters, resolution=resolution).area
def points_along_boundaries(geoseries, distance=1.0):
"""
Generate a shapely MultiPoint of point features along lines at a specified distance
:param geoseries:
:param distance:
:return:
"""
list_points = []
for line3d in iter_line(geoseries):
line = LineString([xy[0:2] for xy in list(line3d.coords)])
current_dist = distance
line_length = line.length
list_points.append(Point(list(line.coords)[0]))
while current_dist < line_length:
list_points.append(line.interpolate(current_dist))
current_dist += distance
list_points.append(Point(list(line.coords)[-1]))
return MultiPoint(list_points)
def lines_touch(one, other, buf=10e-5):
"""Predict the connection of two lines
"""
a = MultiPoint([one.coords[0], one.coords[-1]]).buffer(buf)
b = MultiPoint([other.coords[0], other.coords[-1]]).buffer(buf)
return one.intersects(b) or other.intersects(a)
def bbox_for_track(track):
"""
Get the bounding box for the track.
:param MultiPoint|Point track:
:return:
"""
return BBox(track.bounds[1], track.bounds[0], track.bounds[3], track.bounds[2])
def __mk_track(track_points):
if len(track_points) > 1:
return MultiPoint(track_points)
elif len(track_points) == 1:
return Point(track_points[0])
else:
__LOG.critical(u'Selected GPX have no valid track points')
print u'Selected GPX have no valid track points'
sys.exit(404)
def load_gpx(filename):
"""
:param filename: The file to load the track for.
:return [Point|MultiPoint], BBox:
The point or line read from the GPX track file.
And the bounding box as a 4-tuple.
"""
__LOG.debug(u'Opening GPX file: %s' % filename)
try:
with open(filename, 'r') as gpx_file:
tree = ElementTree.parse(gpx_file)
root = tree.getroot()
except IOError as e:
__LOG.error(u'Failed to read %s: %s' % (filename, e.message))
raise e
tracks = []
for trk in root.findall('{http://www.topografix.com/GPX/1/1}trk'):
for seg in trk.findall('{http://www.topografix.com/GPX/1/1}trkseg'):
track_points = []
for point in seg.findall('{http://www.topografix.com/GPX/1/1}trkpt'):
trk_pt = ([float(point.get('lon')), float(point.get('lat'))])
track_points.append(trk_pt)
tracks.append(__mk_track(track_points))
for trk in root.findall('{http://www.topografix.com/GPX/1/0}trk'):
for seg in trk.findall('{http://www.topografix.com/GPX/1/0}trkseg'):
track_points = []
for point in seg.findall('{http://www.topografix.com/GPX/1/0}trkpt'):
trk_pt = ([float(point.get('lon')), float(point.get('lat'))])
track_points.append(trk_pt)
tracks.append(__mk_track(track_points))
return tracks
test_emptiness.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def test_empty_multipoint(self):
self.assertTrue(sgeom.MultiPoint().is_empty)
test_getitem.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 16
收藏 0
点赞 0
评论 0
def test_index_multigeom(self):
c = [(float(x), float(-x)) for x in range(4)]
g = geometry.MultiPoint(c)
for i in range(-4,4):
self.assertTrue(g[i].equals(geometry.Point(c[i])))
self.assertRaises(IndexError, lambda: g[4])
self.assertRaises(IndexError, lambda: g[-5])
test_getitem.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def test_slice_multigeom(self):
c = [(float(x), float(-x)) for x in range(4)]
g = geometry.MultiPoint(c)
self.failUnlessEqual(type(g[:]), type(g))
self.failUnlessEqual(len(g[:]), len(g))
self.failUnless(g[1:].equals(geometry.MultiPoint(c[1:])))
self.failUnless(g[:-1].equals(geometry.MultiPoint(c[:-1])))
self.failUnless(g[::-1].equals(geometry.MultiPoint(c[::-1])))
self.failUnless(g[4:].is_empty)
test_transform.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def test_multipolygon(self):
g = geometry.MultiPoint([(0, 1), (0, 4)]).buffer(1.0)
h = transform(self.func, g)
self.failUnlessEqual(h.geom_type, 'MultiPolygon')
self.failUnlessAlmostEqual(g.area, h.area)
test_transform.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def test_multipolygon(self):
g = geometry.MultiPoint([(0, 1), (0, 4)]).buffer(1.0)
h = transform(lambda x, y, z=None: (x+1.0, y+1.0), g)
self.failUnlessEqual(h.geom_type, 'MultiPolygon')
self.failUnlessAlmostEqual(g.area, h.area)
self.failUnlessAlmostEqual(h.centroid.x, 1.0)
self.failUnlessAlmostEqual(h.centroid.y, 3.5)
def projectBack(points, proj):
"""
Project a 2D array of XY points from orthographic projection to decimal
degrees.
Args:
points: 2D numpy array of XY points in orthographic projection.
proj: PyProj object defining projection.
Returns:
2D numpy array of Lon/Lat coordinates.
"""
mpoints = MultiPoint(points)
project = partial(
pyproj.transform,
proj,
pyproj.Proj(proj='latlong', datum='WGS84'))
gmpoints = transform(project, mpoints)
coords = []
for point in gmpoints.geoms:
x, y = point.coords[0]
coords.append((x, y))
coords = np.array(coords)
return coords
def projectBack(points, proj):
"""
Project a 2D array of XY points from orthographic projection to decimal
degrees.
Args:
points: 2D numpy array of XY points in orthographic projection.
proj: PyProj object defining projection.
Returns:
2D numpy array of Lon/Lat coordinates.
"""
mpoints = MultiPoint(points)
project = partial(
pyproj.transform,
proj,
pyproj.Proj(proj='latlong', datum='WGS84'))
gmpoints = transform(project, mpoints)
coords = []
for point in gmpoints.geoms:
x, y = point.coords[0]
coords.append((x, y))
coords = np.array(coords)
return coords
def multipoint(self):
"""A shapely.geometry.MultiPoint of the Location members.
"""
return MultiPoint(self._shapely_points())
def cluster_centres(self):
"""Multipoint of cluster geometric centroids.
"""
return MultiPoint([c.centroid for c in self.clusters])
def cluster_named_tuple():
"""Defines a NamedTuple representing a single cluster.
Returns:
Named tuple with the following properties:
label (int): the id of the cluster
centroid: Point representing the cluster centre
geom (MultiPoint or Point): the cluster members
location (errorgeopy.Location): one cluster from the input set
"""
return namedtuple('Cluster', ['label', 'centroid', 'location'])
def gen_points(self, dataframe, map):
"""places points from coords in dataset onto a map
Args:
dataframe: dataset to get coords from
map: map
"""
points = pd.Series(
[Point(self.base_map(mapped_x, mapped_y)) for mapped_x, mapped_y in
zip(dataframe['Longitude'], dataframe['Latitude'])])
crimes = MultiPoint(list(points.values))
polygons = prep(MultiPolygon(list(map['poly'].values)))
return list(filter(polygons.contains, crimes))
def approximate_convex_hull_area(lons, lats):
lon_meters, lat_meters = _get_lon_lat_meters(lons, lats)
lon_lat_meters = list(zip(lon_meters, lat_meters))
return MultiPoint(lon_lat_meters).convex_hull.area / 1000 ** 2
def get_intersections(roads):
"""Calculates the intersection points of all roads
:param roads: List of shapely geometries representing road segments
"""
intersections = []
for road1, road2 in itertools.combinations(roads, 2):
if road1.intersects(road2):
intersection = road1.intersection(road2)
if 'Point' == intersection.type:
intersections.append(intersection)
elif 'MultiPoint' == intersection.type:
intersections.extend([pt for pt in intersection])
elif 'MultiLineString' == intersection.type:
multiLine = [line for line in intersection]
first_coords = multiLine[0].coords[0]
last_coords = multiLine[len(multiLine)-1].coords[1]
intersections.append(Point(first_coords[0], first_coords[1]))
intersections.append(Point(last_coords[0], last_coords[1]))
elif 'GeometryCollection' == intersection.type:
intersections.extend(get_intersections(intersection))
# The unary_union removes duplicate points
unioned = unary_union(intersections)
# Ensure the result is a MultiPoint, since calling functions expect an iterable
if 'Point' == unioned.type:
unioned = MultiPoint([unioned])
return unioned