def make_instance(name=None, is_public=False, url_name=None, point=None, edge_length=600): if name is None: max_instance = Instance.objects.all().aggregate( Max('id'))['id__max'] or 0 name = 'generated$%d' % (max_instance + 1) if url_name is None: max_instance = Instance.objects.all().aggregate( Max('id'))['id__max'] or 0 url_name = 'generated-%d' % (max_instance + 1) p1 = point or Point(0, 0) instance = Instance(name=name, geo_rev=0, is_public=is_public, url_name=url_name) instance.seed_with_dummy_default_role() d = edge_length / 2 instance.bounds = InstanceBounds.create_from_point(p1.x, p1.y, d) instance.save() new_role = Role.objects.create( name='role-%s' % name, instance=instance, rep_thresh=0, default_permission=FieldPermission.READ_ONLY) instance.default_role = new_role instance.save() return instance
def make_instance(name=None, is_public=False, url_name=None, point=None, edge_length=600): if name is None: max_instance = Instance.objects.all().aggregate( Max('id'))['id__max'] or 0 name = 'generated$%d' % (max_instance + 1) if url_name is None: max_instance = Instance.objects.all().aggregate( Max('id'))['id__max'] or 0 url_name = 'generated-%d' % (max_instance + 1) p1 = point or Point(0, 0) instance = Instance(name=name, geo_rev=0, is_public=is_public, url_name=url_name) instance.seed_with_dummy_default_role() d = edge_length / 2 instance.bounds = InstanceBounds.create_from_point(p1.x, p1.y, d) instance.save() new_role = Role.objects.create( name='role-%s' % name, instance=instance, rep_thresh=0, default_permission_level=FieldPermission.READ_ONLY) instance.default_role = new_role instance.save() return instance
def test_create_from_geojson_self_intersection(self): invalid_geojson = { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [[[-78.28857421875, 41.19518982948959], [-76.79443359375, 41.65649719441145], [-78.134765625, 39.04478604850143], [-74.970703125, 40.896905775860006], [-78.28857421875, 41.19518982948959]]] } }] } with self.assertRaises(ValidationError): InstanceBounds.create_from_geojson(json.dumps(invalid_geojson))
def test_create_from_geojson_missing_point(self): # Same as above, but last point is missing invalid_geojson = { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [[[-75.20416259765625, 40.011838968100335], [-75.19866943359375, 40.02551125229787], [-75.20484924316406, 40.029717557833266], [-75.21171569824219, 40.02340800226773], [-75.21102905273438, 40.01762373035351]]] } }] } with self.assertRaises(ValidationError): InstanceBounds.create_from_geojson(json.dumps(invalid_geojson))
def test_create_from_geojson_self_intersection(self): invalid_geojson = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [ -78.28857421875, 41.19518982948959 ], [ -76.79443359375, 41.65649719441145 ], [ -78.134765625, 39.04478604850143 ], [ -74.970703125, 40.896905775860006 ], [ -78.28857421875, 41.19518982948959 ] ] ] } } ] } with self.assertRaises(ValidationError): InstanceBounds.create_from_geojson(json.dumps(invalid_geojson))
def test_create_from_geojson_missing_point(self): # Same as above, but last point is missing invalid_geojson = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [ -75.20416259765625, 40.011838968100335 ], [ -75.19866943359375, 40.02551125229787 ], [ -75.20484924316406, 40.029717557833266 ], [ -75.21171569824219, 40.02340800226773 ], [ -75.21102905273438, 40.01762373035351 ] ] ] } } ] } with self.assertRaises(ValidationError): InstanceBounds.create_from_geojson(json.dumps(invalid_geojson))
def test_create_from_geojson(self): geojson = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [ -75.20416259765625, 40.011838968100335 ], [ -75.19866943359375, 40.02551125229787 ], [ -75.20484924316406, 40.029717557833266 ], [ -75.21171569824219, 40.02340800226773 ], [ -75.21102905273438, 40.01762373035351 ], [ -75.20416259765625, 40.011838968100335 ] ] ] } } ] } i = InstanceBounds.create_from_geojson(json.dumps(geojson)) self.assertTrue(i.geom.valid)
def test_create_from_geojson(self): geojson = { "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [[[-75.20416259765625, 40.011838968100335], [-75.19866943359375, 40.02551125229787], [-75.20484924316406, 40.029717557833266], [-75.21171569824219, 40.02340800226773], [-75.21102905273438, 40.01762373035351], [-75.20416259765625, 40.011838968100335]]] } }] } i = InstanceBounds.create_from_geojson(json.dumps(geojson)) self.assertTrue(i.geom.valid)
def handle(self, *args, **options): if len(args) != 1: raise Exception( 'Expected instance name as the first argument') name = args[0] if not options['user']: raise Exception('An admin user must be specified. with "--user"') if options.get('center', None) and options.get('geojson', None): raise Exception('You must specifiy only one of ' '"center" and "geojson"') elif (not options.get('center', None) and not options.get('geojson', None)): raise Exception('You must specifiy at least one of ' '"center" and "geojson"') if options['center']: center = options['center'].split(',') if len(center) != 2: raise Exception('Center should be a lon,lat pair') center_pt = Point(float(center[0]), float(center[1]), srid=4326) # Bounding box built in web mercator to have units in meters center_pt.transform(3857) x = center_pt.x y = center_pt.y instance_bounds = InstanceBounds.create_from_point(x, y) else: geom = GEOSGeometry(open(options['geojson'], srid=4326).read()) instance_bounds = InstanceBounds.objects.create(geom=geom) if not options.get('url_name', None): raise Exception('You must specify a "url_name" starting with a ' 'lowercase letter and containing lowercase ' 'letters, numbers, and dashes ("-")') url_name = options.get('url_name') instance = Instance( config={}, name=name, bounds=instance_bounds, is_public=True, url_name=url_name) instance.seed_with_dummy_default_role() instance.full_clean() instance.save() instance.boundaries = Boundary.objects.filter( geom__intersects=instance_bounds.geom) role = Role.objects.create( name='user', instance=instance, rep_thresh=0, default_permission=FieldPermission.WRITE_DIRECTLY) create_stewardship_udfs(instance) add_species_to_instance(instance) add_default_permissions(instance, roles=[role]) eco_benefits_conversion = \ BenefitCurrencyConversion.get_default_for_point(Point(x, y)) if eco_benefits_conversion: eco_benefits_conversion.save() instance.eco_benefits_conversion = eco_benefits_conversion instance.default_role = role instance.save() user = User.objects.get(username=options['user']) InstanceUser( instance=instance, user=user, role=role, admin=True).save_with_user(user)
def handle(self, *args, **options): if len(args) != 1: raise Exception('Expected instance name as the first argument') name = args[0] if not options['user']: raise Exception('An admin user must be specified. with "--user"') if options.get('center', None) and options.get('geojson', None): raise Exception('You must specifiy only one of ' '"center" and "geojson"') elif (not options.get('center', None) and not options.get('geojson', None)): raise Exception('You must specifiy at least one of ' '"center" and "geojson"') if options['center']: center = options['center'].split(',') if len(center) != 2: raise Exception('Center should be a lon,lat pair') center_pt = Point(float(center[0]), float(center[1]), srid=4326) # Bounding box built in web mercator to have units in meters center_pt.transform(3857) x = center_pt.x y = center_pt.y instance_bounds = InstanceBounds.create_from_point(x, y) else: geom = GEOSGeometry(open(options['geojson'], srid=4326).read()) instance_bounds = InstanceBounds.objects.create(geom=geom) if not options.get('url_name', None): raise Exception('You must specify a "url_name" starting with a ' 'lowercase letter and containing lowercase ' 'letters, numbers, and dashes ("-")') url_name = options.get('url_name') instance = Instance(config={}, name=name, bounds=instance_bounds, is_public=True, url_name=url_name) instance.seed_with_dummy_default_role() instance.full_clean() instance.save() instance.boundaries = Boundary.objects.filter( geom__intersects=instance_bounds.geom) role = Role.objects.create( name='user', instance=instance, rep_thresh=0, default_permission_level=FieldPermission.WRITE_DIRECTLY) create_stewardship_udfs(instance) add_species_to_instance(instance) add_default_permissions(instance, roles=[role]) add_instance_permissions([role]) eco_benefits_conversion = \ BenefitCurrencyConversion.get_default_for_instance(instance) if eco_benefits_conversion: eco_benefits_conversion.save() instance.eco_benefits_conversion = eco_benefits_conversion instance.default_role = role instance.save() user = User.objects.get(username=options['user']) InstanceUser(instance=instance, user=user, role=role, admin=True).save_with_user(user)