本文整理汇总了Python中shapely.wkt.loads方法的典型用法代码示例。如果您正苦于以下问题:Python wkt.loads方法的具体用法?Python wkt.loads怎么用?Python wkt.loads使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shapely.wkt
的用法示例。
在下文中一共展示了wkt.loads方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _densify
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def _densify(self, geom, segment):
"""
Returns densified geoemtry with segments no longer than `segment`.
"""
# temporary solution for readthedocs fail. - cannot mock osgeo
try:
from osgeo import ogr
except ModuleNotFoundError:
import warnings
warnings.warn("OGR (GDAL) is required.")
poly = geom
wkt = geom.wkt # shapely Polygon to wkt
geom = ogr.CreateGeometryFromWkt(wkt) # create ogr geometry
geom.Segmentize(segment) # densify geometry by 2 metres
geom.CloseRings() # fix for GDAL 2.4.1 bug
wkt2 = geom.ExportToWkt() # ogr geometry to wkt
try:
new = loads(wkt2) # wkt to shapely Polygon
return new
except Exception:
return poly
示例2: make_layers
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def make_layers(shapes, geom_dicts=False):
print("Creating layers with 10 shapes each")
layers = []
i = 0
features = []
for shape in shapes:
try:
geom = loads_wkt(shape.strip())
if geom_dicts:
feature = {"geometry": mapping(geom), "properties": {}}
else:
feature = {"geometry": geom, "properties": {}}
features.append(feature)
if i >= 10:
layers.append(features)
features = []
i = 0
i += 1
except:
pass
layers.append(features)
return layers
示例3: test_invalid_geometry_make_valid
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def test_invalid_geometry_make_valid(self):
from mapbox_vector_tile import encode
from mapbox_vector_tile.encoder import on_invalid_geometry_make_valid
import shapely.geometry
import shapely.wkt
geometry = 'POLYGON ((10 10, 20 10, 20 20, 15 15, 15 5, 10 10))'
shape = shapely.wkt.loads(geometry)
self.assertFalse(shape.is_valid)
feature = dict(geometry=shape, properties={})
source = dict(name='layername', features=[feature])
pbf = encode(source,
on_invalid_geometry=on_invalid_geometry_make_valid)
result = decode(pbf)
self.assertEqual(1, len(result['layername']['features']))
valid_geometry = result['layername']['features'][0]['geometry']
self.assertTrue(valid_geometry['type'], 'MultiPolygon')
multipolygon = shapely.geometry.shape(valid_geometry)
self.assertTrue(multipolygon.geom_type, 'MultiPolygon')
self.assertTrue(multipolygon.is_valid)
for poly in multipolygon.geoms:
self.assertTrue(poly.is_valid)
示例4: test_bowtie_self_touching
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def test_bowtie_self_touching(self):
from mapbox_vector_tile import encode
from mapbox_vector_tile.encoder import on_invalid_geometry_make_valid
import shapely.geometry
import shapely.wkt
bowtie = ('POLYGON ((0 0, 0 2, 1 1, 2 2, 2 0, 1 1, 0 0))')
shape = shapely.wkt.loads(bowtie)
self.assertFalse(shape.is_valid)
feature = dict(geometry=shape, properties={})
source = dict(name='layername', features=[feature])
pbf = encode(source,
on_invalid_geometry=on_invalid_geometry_make_valid)
result = decode(pbf)
self.assertEqual(1, len(result['layername']['features']))
valid_geometries = result['layername']['features'][0]['geometry']
multipolygon = shapely.geometry.shape(valid_geometries)
self.assertEqual(2, len(multipolygon.geoms))
shape1, shape2 = multipolygon.geoms
self.assertTrue(shape1.is_valid)
self.assertTrue(shape2.is_valid)
self.assertGreater(shape1.area, 0)
self.assertGreater(shape2.area, 0)
示例5: test_validate_generates_rounding_error
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def test_validate_generates_rounding_error(self):
from mapbox_vector_tile import encode
from mapbox_vector_tile.encoder import on_invalid_geometry_make_valid
import shapely.geometry
import shapely.wkt
bowtie = ('POLYGON((0 0, 1 1, 0 1, 1 0, 0 0))')
shape = shapely.wkt.loads(bowtie)
self.assertFalse(shape.is_valid)
feature = dict(geometry=shape, properties={})
source = dict(name='layername', features=[feature])
pbf = encode(source,
on_invalid_geometry=on_invalid_geometry_make_valid)
result = decode(pbf)
features = result['layername']['features']
self.assertEqual(1, len(features))
self.assertEqual(features[0]['geometry']['type'], 'Polygon')
shape = shapely.geometry.shape(features[0]['geometry'])
self.assertEqual(shape.geom_type, 'Polygon')
self.assertTrue(shape.is_valid)
self.assertGreater(shape.area, 0)
示例6: test_flipped_geometry_produces_multipolygon
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def test_flipped_geometry_produces_multipolygon(self):
from mapbox_vector_tile import encode
from mapbox_vector_tile.encoder import on_invalid_geometry_make_valid
import shapely.wkt
shape = shapely.wkt.loads('POLYGON ((3449 1939, 3476 1967, 3473 1996, 3483 2027, 3542 2119, 3538 2160, 3563 2233, 3602 2255, 3639 2326, 3629 2388, 3573 2455, 3594 2493, 3558 2533, 3573 2549, 3518 2572, 3502 2592, 3505 2607, 3513 2614, 3535 2616, 3537 2610, 3535 2602, 3537 2599, 3548 2607, 3551 2636, 3528 2634, 3537 2668, 3549 2670, 3528 2711, 3550 2667, 3532 2635, 3550 2641, 3553 2613, 3549 2602, 3540 2596, 3512 2610, 3506 2589, 3576 2552, 3576 2543, 3563 2535, 3596 2506, 3597 2494, 3587 2469, 3589 2451, 3636 2385, 3644 2326, 3605 2251, 3566 2230, 3547 2122, 3482 2014, 3479 1966, 3455 1944, 3458 1910, 3449 1902, 3449 1939))') # noqa
features = [dict(geometry=shape, properties={})]
pbf = encode({'name': 'foo', 'features': features},
on_invalid_geometry=on_invalid_geometry_make_valid)
result = decode(pbf)
features = result['foo']['features']
self.assertEqual(1, len(features))
geom = shapely.geometry.shape(features[0]['geometry'])
self.assertEqual(features[0]['geometry']['type'], 'MultiPolygon')
self.assertEqual(geom.geom_type, 'MultiPolygon')
self.assertTrue(geom.is_valid)
for poly in geom.geoms:
self.assertTrue(poly.is_valid)
示例7: test_polygon_disconnected_inner
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def test_polygon_disconnected_inner(self):
geom = wkt.loads("""POLYGON(
(0 0, 5 0, 5 5, 0 5, 0 0),
(1 1, 1 2, 2 2, 1 1),
(2 1, 2 2, 3 2, 2 1),
(3 1, 3 2, 4 2, 3 1),
(1 2, 1 3, 2 3, 1 2),
(2 2, 2 3, 3 3, 2 2),
(3 2, 3 3, 4 3, 3 2),
(1 3, 1 4, 2 4, 1 3),
(2 3, 2 4, 3 4, 2 3),
(3 3, 3 4, 4 4, 3 3)
)""")
self.assertFalse(geom.is_valid)
fixed = make_it_valid(geom)
self.assertTrue(fixed.is_valid)
self.assertEquals(20.5, fixed.area)
示例8: test_polygon_inners_crossing_outer
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def test_polygon_inners_crossing_outer(self):
geom = wkt.loads("""POLYGON (
(2325 1015, 2329 1021, 2419 1057, 2461 944, 2369 907, 2325 1015),
(2329 1012, 2370 909, 2457 944, 2417 1050, 2329 1012),
(2410 1053, 2410 1052, 2412 1053, 2411 1054, 2410 1053),
(2378 1040, 2378 1039, 2379 1040, 2379 1041, 2378 1040),
(2369 1037, 2370 1036, 2371 1036, 2371 1038, 2369 1037),
(2361 1034, 2362 1033, 2363 1033, 2363 1034, 2361 1034),
(2353 1031, 2354 1029, 2355 1030, 2354 1031, 2353 1031),
(2337 1024, 2338 1023, 2339 1023, 2338 1025, 2337 1024)
)""")
self.assertFalse(geom.is_valid)
fixed = make_it_valid(geom)
self.assertTrue(fixed.is_valid)
# different versions of GEOS hit this bug in slightly different ways,
# meaning that some inners get included and some don't, depending on
# the version. therefore, we need quite a wide range of acceptable
# answers.
#
# the main part of this polygon (outer - largest inner) has area 1551,
# and the smaller inners sum up to area 11, so we'll take +/-6 from
# 1545.
self.assertAlmostEqual(1545, fixed.area, delta=6)
示例9: _parse_geoms
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def _parse_geoms(self, **kwargs):
""" Finds supported geometry types, parses them and returns the bbox """
bbox = kwargs.get('bbox', None)
wkt_geom = kwargs.get('wkt', None)
geojson = kwargs.get('geojson', None)
if bbox is not None:
g = box(*bbox)
elif wkt_geom is not None:
g = wkt.loads(wkt_geom)
elif geojson is not None:
g = shape(geojson)
else:
return None
if self.proj is None:
return g
else:
return self._reproject(g, from_proj=kwargs.get('from_proj', 'EPSG:4326'))
示例10: latlon_to_shp
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def latlon_to_shp(lon, lat, shapefile):
shapefile = str(shapefile)
schema = {'geometry': 'Point',
'properties': {'id': 'str'}}
wkt = loads('POINT ({} {})'.format(lon, lat))
with collection(shapefile, "w",
crs=from_epsg(4326),
driver="ESRI Shapefile",
schema=schema) as output:
output.write({'geometry': mapping(wkt),
'properties': {'id': '1'}})
示例11: gdf_to_json_geometry
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def gdf_to_json_geometry(gdf):
"""Function to parse features from GeoDataFrame in such a manner
that rasterio wants them"""
#
# try:
# gdf.geometry.values[0].type
# features = [json.loads(gdf.to_json())['features'][0]['geometry']]
# except AttributeError:
# ids, feats =[], []
# for i, feat in enumerate(gdf.geometry.values[0]):
# ids.append(i)
# feats.append(feat)
#
# gdf = gpd.GeoDataFrame({'id': ids,
# 'geometry': feats},
# geometry='geometry',
# crs = gdf.crs
# )
geojson = json.loads(gdf.to_json())
return [feature['geometry'] for feature in geojson['features']
if feature['geometry']]
示例12: create_aoi_str
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def create_aoi_str(aoi_wkt):
'''A helper function to create a scihub API compliant AOI string
Args:
aoi (str): is WKT representation of the Area Of Interest
Returns:
str: Copernicus' scihub compliant AOI string
'''
geom = loads(aoi_wkt)
if geom.geom_type == 'Point':
aoi_str = "( footprint:\"Intersects({}, {})\")".format(geom.y, geom.x)
else:
# simplify geometry
aoi_convex = geom.convex_hull
# create scihub-confrom aoi string
aoi_str = '( footprint:\"Intersects({})\")'.format(aoi_convex)
return aoi_str
示例13: test_is_pickleable
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def test_is_pickleable(self):
edge_point = json.loads("""{
"id": "argo-profiles-5903995(46, 0)",
"time": "2012-10-15T14:24:04Z",
"point": "-33.467 29.728",
"sea_water_temperature": 24.5629997253,
"sea_water_temperature_depth": 2.9796258642,
"wind_speed": null,
"sea_water_salinity": null,
"sea_water_salinity_depth": null,
"platform": 4,
"device": 3,
"fileurl": "ftp://podaac-ftp.jpl.nasa.gov/allData/insitu/L2/spurs1/argo/argo-profiles-5903995.nc"
}""")
point = DomsPoint.from_edge_point(edge_point)
self.assertIsNotNone(pickle.dumps(point))
示例14: test_intersections_correct_when_all_overlapping
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def test_intersections_correct_when_all_overlapping(four_square_grid, square):
indexed = IndexedGeometries(four_square_grid)
overlaps = indexed.intersections(square)
expected_polygons = [
wkt.loads(p)
for p in [
"POLYGON ((0.5 1, 1 1, 1 0.5, 0.5 0.5, 0.5 1))",
"POLYGON ((1 1.5, 1 1, 0.5 1, 0.5 1.5, 1 1.5))",
"POLYGON ((1 0.5, 1 1, 1.5 1, 1.5 0.5, 1 0.5))",
"POLYGON ((1 1, 1 1.5, 1.5 1.5, 1.5 1, 1 1))",
]
]
for p in expected_polygons:
assert any(overlap == p for overlap in overlaps)
for p in overlaps:
assert any(p == expected for expected in expected_polygons)
示例15: _loadGeometry
# 需要导入模块: from shapely import wkt [as 别名]
# 或者: from shapely.wkt import loads [as 别名]
def _loadGeometry(self, geometrySpec):
"""
A private method to convert a (E)WKB or (E)WKT to a Shapely geometry.
"""
if type(geometrySpec) is str and geometrySpec.startswith('POLYGON Z'):
try:
geometry = load_wkt(geometrySpec)
except Exception:
geometry = None
else:
try:
geometry = load_wkb(geometrySpec)
except Exception:
geometry = None
if geometry is None:
raise ValueError('Failed to convert WKT or WKB to a Shapely geometry')
return geometry