Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_invalid_multipolygon(self):
with self.assertRaises(ValueError) as cm:
geojson.MultiPolygon([1], validate=True)
self.assertIn("Each polygon must be a list of linear rings",
str(cm.exception))
poly1 = [(2.38, 57.322), (23.194, -20.28),
(-120.43, 19.15), (25.44, -17.91)]
poly2 = [(2.38, 57.322), (23.194, -20.28),
(-120.43, 19.15), (2.38, 57.322)]
multipoly = geojson.MultiPolygon([poly1, poly2])
self.assertEqual(multipoly.is_valid, False)
def test_geometrycollection(self):
p1 = geojson.Point((-115.11, 37.11))
p2 = geojson.Point((-115.22, 37.22))
ln = geojson.LineString(
[(-115.3, 37.3), (-115.4, 37.4), (-115.5, 37.5)])
g = geojson.MultiPolygon([
([(3.78, 9.28), (-130.91, 1.52), (35.12, 72.234), (3.78, 9.28)],),
([(23.18, -34.29), (-1.31, -4.61),
(3.41, 77.91), (23.18, -34.29)],)])
itr = coords(geojson.GeometryCollection([p1, p2, ln, g]))
pairs = set(itr)
self.assertEqual(pairs, set([
(-115.11, 37.11), (-115.22, 37.22),
(-115.3, 37.3), (-115.4, 37.4), (-115.5, 37.5),
(3.78, 9.28), (-130.91, 1.52), (35.12, 72.234), (3.78, 9.28),
(23.18, -34.29), (-1.31, -4.61), (3.41, 77.91), (23.18, -34.29)
]))
def test_project_can_be_generated_as_dto(self):
if self.skip_tests:
return
# Arrange
self.update_project_with_info()
# Act
project_dto = self.test_project.as_dto_for_mapping("en", False)
# Assert
self.assertIsInstance(project_dto.area_of_interest, geojson.MultiPolygon)
self.assertIsInstance(project_dto.tasks, geojson.FeatureCollection)
# TODO test for project info
# self.assertEqual(project_dto.project_name, 'Test')
self.assertEqual(project_dto.project_id, self.test_project.id)
def test_cant_add_task_if_not_supplied_feature_type(self):
# Arrange
invalid_feature = geojson.MultiPolygon(
[[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 10.33)]]
)
# Arrange
with self.assertRaises(InvalidGeoJson):
Task.from_geojson_feature(1, invalid_feature)
def from_geojson_feature(cls, task_id, task_feature):
"""
Constructs and validates a task from a GeoJson feature object
:param task_id: Unique ID for the task
:param task_feature: A geoJSON feature object
:raises InvalidGeoJson, InvalidData
"""
if type(task_feature) is not geojson.Feature:
raise InvalidGeoJson('Task: Invalid GeoJson should be a feature')
task_geometry = task_feature.geometry
if type(task_geometry) is not geojson.MultiPolygon:
raise InvalidGeoJson('Task: Geometry must be a MultiPolygon')
is_valid_geojson = geojson.is_valid(task_geometry)
if is_valid_geojson['valid'] == 'no':
raise InvalidGeoJson(f"Task: Invalid MultiPolygon - {is_valid_geojson['message']}")
task = cls()
try:
task.x = task_feature.properties['x']
task.y = task_feature.properties['y']
task.zoom = task_feature.properties['zoom']
task.splittable = task_feature.properties['splittable']
except KeyError as e:
raise InvalidData(f'Task: Expected property not found: {str(e)}')
if 'extra_properties' in task_feature.properties:
def toEWKT(gj):
"Convert geojson to EWKT"
mp = geojson.MultiPolygon(gj['coordinates'])
return 'SRID=4326;' + asShape(mp).wkt
crds.append([pts])
# FIXME: this is a good idea, but really slow...
# the is_clockwise() code could be cythonized, maybe that would help?
# # geojson polygons should be counter-clockwise
# if is_clockwise(poly):
# p.reverse()
features = []
if land_coords:
land = Feature(id="1",
properties={'name': 'Shoreline Polys'},
geometry=MultiPolygon(land_coords)
)
lakes = Feature(id="2",
properties={'name': 'Lakes'},
geometry=MultiPolygon(lake_coords)
)
features.append(land)
features.append(lakes)
return FeatureCollection(features)
dbf_bak.close()
#shptype = str(shp_bak.type).strip("")
if 'Polygon' in str(shp_bak.type):
ftype = 'Polygon'
elif 'Point' in str(type(shp_bak.type)):
raise NotImplementedError('Point data is not implemented yet')
if ftype == "Polygon":
feats = []
for idx in range(len(chains)):
chain = chains[idx]
if len(chain.parts) > 1:
#shptype = 'MultiPolygon'
geom = gj.MultiPolygon([ [[ list(coord) for coord in part]] for part
in chain.parts])
else:
#shptype = 'Polygon'
geom = gj.Polygon(coordinates = [ [ list(coord) for coord in
part] for part in chain.parts])
prop = {head: val for head,val in zip(dbf_bak.header,
dbftable[idx])}
bbox = chain.bbox
feats.append(gj.Feature(None, geometry=geom, properties=prop, bbox=bbox))
return gj.FeatureCollection(feats, bbox = shp_bak.bbox )
def from_geojson_feature(geo):
s_feature = icp_model.Feature()
s_feature.fieldNames = list(geo['properties'].keys())
s_feature.fieldValues = list(geo['properties'].values())
if type(geo['geometry']) in (geojson.Point, geojson.MultiPoint):
s_feature.geometry = __from_geojson_point(geo['geometry'])
elif type(geo['geometry']) in (geojson.LineString, geojson.MultiLineString):
s_feature.geometry = __from_geojson_line(geo['geometry'])
elif type(geo['geometry']) in (geojson.Polygon, geojson.MultiPolygon):
s_feature.geometry = __from_geojson_polygon(geo['geometry'])
return s_feature