def test_cnml(self): """ test CNML """ from nodeshot.interop.sync.synchronizers import Cnml from nodeshot.networking.net.models import Device, Ip from nodeshot.networking.links.models import Link layer = Layer.objects.external()[0] layer.new_nodes_allowed = False layer.save() layer = Layer.objects.get(pk=layer.pk) url = '%s/cnml1.xml' % self.STATIC_TEST_URL external = LayerExternal(layer=layer) external.synchronizer_path = 'nodeshot.interop.sync.synchronizers.Cnml' external._reload_schema() external.config = {"url": url} external.full_clean() external.save() ip_count = Ip.objects.count() link_count = Link.objects.count() output = capture_output(management.call_command, ['sync', 'vienna'], kwargs={'verbosity': 0}) for status in Cnml.STATUS_MAPPING.keys(): self.assertEqual(Status.objects.filter(slug=status).count(), 1) # ensure following text is in output self.assertIn('3 nodes added', output) self.assertIn('0 nodes changed', output) self.assertIn('3 total external', output) self.assertIn('3 total local', output) # start checking DB too nodes = layer.node_set.all() # ensure all nodes have been imported self.assertEqual(nodes.count(), 3) # check one particular node has the data we expect it to have node = Node.objects.get(name='TOLOArtzubi') self.assertEqual(node.address, '') geometry = GEOSGeometry('POINT (-2.116230 43.146330)') self.assertTrue( node.geometry.equals_exact(geometry) or node.geometry.equals(geometry)) self.assertEqual(node.elev, 15) self.assertEqual(node.status.slug, 'building') self.assertEqual(node.data['cnml_id'], '55349') # check devices self.assertIn('12 devices added', output) self.assertEqual(Device.objects.filter(node__layer=layer).count(), 12) # check interfaces device = Device.objects.get(data={'cnml_id': 49635}) self.assertEqual(device.interface_set.count(), 3) self.assertIn('21 interfaces added', output) self.assertEqual(Ip.objects.count(), ip_count + 21) # check links self.assertEqual(Link.objects.count(), link_count + 9) # --- repeat with different XML --- # url = '%s/cnml2.xml' % self.STATIC_TEST_URL external.config = {"url": url} external.full_clean() external.save() output = capture_output(management.call_command, ['sync', 'vienna'], kwargs={'verbosity': 0}) # ensure following text is in output self.assertIn('3 nodes unmodified', output) self.assertIn('0 nodes deleted', output) self.assertIn('0 nodes changed', output) self.assertIn('3 total external', output) self.assertIn('3 total local', output) self.assertIn('0 devices added', output) self.assertIn('2 devices deleted', output) # check devices self.assertEqual(nodes.count(), 3) self.assertEqual(Device.objects.filter(node__layer=layer).count(), 10) # check interfaces self.assertEqual(device.interface_set.count(), 2) self.assertIn('0 interfaces added', output) self.assertIn('1 interfaces deleted', output) self.assertEqual(Ip.objects.count(), ip_count + 18)
def save_model(self, request, obj, form, change): # pragma no cover geom_merc = obj.the_geom_webmercator obj.the_geom = geom_merc.transform(ct=4326, clone=True) obj.the_geog = GEOSGeometry(obj.the_geom.wkt) obj.save()
def addRunlet(run, old, new): if ((old.lat == new.lat) and (old.lon == new.lon) and (run['runlets'] == 0)): return path_id = getNearestPath(old) if (path_id == None): path_id = -1 old_lat = old.lat old_lon = old.lon t0 = old.t new_lat = new.lat new_lon = new.lon tn = new.t if (old.dirTag != new.dirTag): old_lat = new_lat old_lon = new_lon t0 = tn sql = "insert into runlet (run_id,lat0,lon0,loc0,t0,latn,lonn,locn,tn,path_id,distance) values (" + str( run['id']) + "," + str(old_lat) + "," + str( old_lon) + ", ST_SetSRID(ST_MakePoint(" + str(old_lon) + "," + str( old_lat ) + " )," + srid + ")," + str(t0) + "," + str(new_lat) + "," + str( new_lon) + ",ST_SetSRID(ST_MakePoint(" + str( new_lon) + "," + str(new_lat) + " )," + srid + ") ," + str( tn) + "," + str(path_id) + ",0) returning id" cur = old.db.cursor() cur.execute(sql) old.db.commit() id = cur.fetchone()[0] GEOSGeom0 = GEOSGeometry("POINT(" + str(old.lon) + " " + str(old.lat) + ")") GEOSGeom0.srid = 4326 GEOSGeomn = GEOSGeometry("POINT(" + str(new.lon) + " " + str(new.lat) + ")") GEOSGeomn.srid = 4326 GEOSGeom0.transform(ct) GEOSGeomn.transform(ct) dist = GEOSGeom0.distance(GEOSGeomn) sql = "update runlet set distance=%s where id=%s" cur.execute(sql, ( dist, id, )) old.db.commit() freq = int((t - run['startTime']) / (run['runlets'] + 1)) sql = "update run set distance = distance + %s, runlets = runlets + 1, freq = %s where id = " + str( run['id']) cur.execute(sql, ( dist, freq, )) old.db.commit() """
def save(self, *args, **kwargs): self.location = GEOSGeometry('POINT({} {})'.format( self.longitude, self.latitude)) super().save(*args, **kwargs)
def post(self, request, *args, **kwargs): comm_form = self.community_form_class(request.POST, label_suffix="") addr_form = self.address_form_class(request.POST, label_suffix="") if comm_form.is_valid(): entryForm = comm_form.save(commit=False) # This returns an array of Django GEOS Polygon types polyArray = comm_form.data["census_blocks_polygon_array"] if polyArray is not None and polyArray != "": polyArray = polyArray.split("|") newPolyArr = [] # union them one at a time- does not work for stringPolygon in polyArray: new_poly = GEOSGeometry(stringPolygon, srid=4326) newPolyArr.append(new_poly) mpoly = MultiPolygon(newPolyArr) polygonUnion = mpoly.unary_union polygonUnion.normalize() # if one polygon is returned, create a multipolygon if polygonUnion.geom_typeid == 3: polygonUnion = MultiPolygon(polygonUnion) entryForm.census_blocks_polygon = polygonUnion if self.kwargs["drive"]: drive = Drive.objects.get(slug=self.kwargs["drive"]) if drive: entryForm.drive = drive entryForm.organization = drive.organization if entryForm.organization: if self.request.user.is_member(entryForm.organization.id): entryForm.admin_approved = True else: # check if user is on the allowlist allowlist_entry = AllowList.objects.filter( organization=entryForm.organization.id, email=self.request.user.email, ) if allowlist_entry: # approve this entry entryForm.admin_approved = True # TODO: Determine role of drive tokens (one time link, etc.) # if self.kwargs["token"]: # token = DriveToken.objects.get(token=self.kwargs["token"]) # if token: # entryForm.drive = token.drive # entryForm.organization = token.drive.organization # # if user has a token and drive is active, auto approve submission # if token.drive.is_active: # entryForm.admin_approved = True entryForm.save() if addr_form.is_valid(): addrForm = addr_form.save(commit=False) addrForm.entry = entryForm addrForm.save() m_uuid = str(entryForm.entry_ID).split("-")[0] if not entryForm.drive: self.success_url = reverse_lazy("main:thanks", kwargs={"map_id": m_uuid}) else: self.success_url = reverse_lazy( "main:thanks", kwargs={ "map_id": m_uuid, "slug": entryForm.organization.slug, "drive": entryForm.drive.slug, }, ) return HttpResponseRedirect(self.success_url) context = { "comm_form": comm_form, "addr_form": addr_form, "mapbox_key": os.environ.get("DISTR_MAPBOX_KEY"), "mapbox_user_name": os.environ.get("MAPBOX_USER_NAME"), } return render(request, self.template_name, context)
def setUp(self): """Shared objects. Bugs with TestMommy vs django-polymorphic: https://github.com/django-polymorphic/django-polymorphic/issues/280 https://github.com/django-polymorphic/django-polymorphic/issues/109 polymorphic/models.py", line 112, in get_real_instance_class and not issubclass(model, self.__class__._meta.proxy_for_model): TypeError: issubclass() arg 2 must be a class or tuple of classes """ self.taxon0 = mommy.make(Taxon, name_id=1000, name="name0", _fill_optional=['rank', 'eoo']) self.taxon0.save() self.taxon1 = mommy.make(Taxon, name_id=1001, name="name1", _fill_optional=['rank', 'eoo']) self.taxon1.save() self.taxon2 = mommy.make(Taxon, name_id=1002, name="name2", _fill_optional=['rank', 'eoo']) self.taxon2.save() self.user = get_user_model().objects.create_superuser( username="******", email="*****@*****.**", password="******") self.user.save() self.tae = occ_models.TaxonAreaEncounter.objects.create( taxon=self.taxon0, encountered_on=timezone.now(), encountered_by=self.user, code="area1", label="Area 1", point=GEOSGeometry('POINT (115 -32)', srid=4326), geom=GEOSGeometry( '{ "type": "Polygon", "coordinates": [ [ [ 116.586914, -32.916485 ], ' '[ 116.586914, -30.977609 ], [ 120.27832, -30.977609 ], ' '[ 120.27832, -32.916485 ], [ 116.586914, -32.916485 ] ] ] }') ) self.tae1 = occ_models.TaxonAreaEncounter.objects.create( taxon=self.taxon0, encountered_on=timezone.now(), encountered_by=self.user, point=GEOSGeometry('POINT (115 -32)', srid=4326)) self.asssp1 = occ_models.AssociatedSpecies.objects.create( encounter=self.tae, taxon=self.taxon1) self.asssp1.save() self.asssp2 = occ_models.AssociatedSpecies.objects.create( encounter=self.tae, taxon=self.taxon2) self.asssp2.save() self.fh1 = occ_models.FireHistory.objects.create( encounter=self.tae, last_fire_date=timezone.now(), fire_intensity=occ_models.FireHistory.HMLN_HIGH) self.fh1.save() self.vc = occ_models.VegetationClassification.objects.create( encounter=self.tae, level1="lsdkfjlksdf", level2="slkdjflskdjf", level3="sdklsjdf", level4="sldkfjslkdjfskdf") self.vc.save() self.fatt = occ_models.FileAttachment.objects.create( encounter=self.tae, attachment=SimpleUploadedFile('testfile.txt', b'These are the file contents.'), title="test", author=self.user) self.fatt.save() self.fatt1 = occ_models.FileAttachment.objects.create( encounter=self.tae, attachment=SimpleUploadedFile('testfile.txt', b'These are the file contents.'), title="test", author=self.user, confidential=True) self.fatt1.save() self.aa0 = occ_models.AreaAssessment.objects.create( encounter=self.tae, area_surveyed_m2=None, survey_duration_min=None) self.aa0.save() self.aa1 = occ_models.AreaAssessment.objects.create( encounter=self.tae, area_surveyed_m2=200, survey_duration_min=None) self.aa1.save() self.aa2 = occ_models.AreaAssessment.objects.create( encounter=self.tae, area_surveyed_m2=532, survey_duration_min=47) self.aa2.save() # TODO add cons threat self.habcond = occ_models.HabitatCondition.objects.get_or_create( encounter=self.tae, pristine_percent=12, excellent_percent=23, very_good_percent=34, good_percent=45, degraded_percent=56, completely_degraded_percent=67, ) self.client.force_login(self.user)
class DistanceTest(unittest.TestCase): # A point we are testing distances with -- using a WGS84 # coordinate that'll be implicitly transormed to that to # the coordinate system of the field, EPSG:32140 (Texas South Central # w/units in meters) stx_pnt = GEOSGeometry('POINT (-95.370401017314293 29.704867409475465)', 4326) # Another one for Australia au_pnt = GEOSGeometry('POINT (150.791 -34.4919)', 4326) def get_names(self, qs): cities = [c.name for c in qs] cities.sort() return cities def test01_init(self): "Initialization of distance models." # Loading up the cities. def load_cities(city_model, data_tup): for name, x, y in data_tup: city_model(name=name, point=Point(x, y, srid=4326)).save() def load_interstates(imodel, data_tup): for name, wkt in data_tup: imodel(name=name, path=wkt).save() load_cities(SouthTexasCity, stx_cities) load_cities(SouthTexasCityFt, stx_cities) load_cities(AustraliaCity, au_cities) self.assertEqual(9, SouthTexasCity.objects.count()) self.assertEqual(9, SouthTexasCityFt.objects.count()) self.assertEqual(11, AustraliaCity.objects.count()) # Loading up the South Texas Zip Codes. for name, wkt in stx_zips: poly = GEOSGeometry(wkt, srid=4269) SouthTexasZipcode(name=name, poly=poly).save() CensusZipcode(name=name, poly=poly).save() self.assertEqual(4, SouthTexasZipcode.objects.count()) self.assertEqual(4, CensusZipcode.objects.count()) # Loading up the Interstates. load_interstates(Interstate, interstates) load_interstates(SouthTexasInterstate, stx_interstates) self.assertEqual(1, Interstate.objects.count()) self.assertEqual(1, SouthTexasInterstate.objects.count()) @no_spatialite def test02_dwithin(self): "Testing the `dwithin` lookup type." # Distances -- all should be equal (except for the # degree/meter pair in au_cities, that's somewhat # approximate). tx_dists = [(7000, 22965.83), D(km=7), D(mi=4.349)] au_dists = [(0.5, 32000), D(km=32), D(mi=19.884)] # Expected cities for Australia and Texas. tx_cities = ['Downtown Houston', 'Southside Place'] au_cities = ['Mittagong', 'Shellharbour', 'Thirroul', 'Wollongong'] # Performing distance queries on two projected coordinate systems one # with units in meters and the other in units of U.S. survey feet. for dist in tx_dists: if isinstance(dist, tuple): dist1, dist2 = dist else: dist1 = dist2 = dist qs1 = SouthTexasCity.objects.filter(point__dwithin=(self.stx_pnt, dist1)) qs2 = SouthTexasCityFt.objects.filter(point__dwithin=(self.stx_pnt, dist2)) for qs in qs1, qs2: self.assertEqual(tx_cities, self.get_names(qs)) # Now performing the `dwithin` queries on a geodetic coordinate system. for dist in au_dists: if isinstance(dist, D) and not oracle: type_error = True else: type_error = False if isinstance(dist, tuple): if oracle: dist = dist[1] else: dist = dist[0] # Creating the query set. qs = AustraliaCity.objects.order_by('name') if type_error: # A ValueError should be raised on PostGIS when trying to pass # Distance objects into a DWithin query using a geodetic field. self.assertRaises(ValueError, AustraliaCity.objects.filter(point__dwithin=(self.au_pnt, dist)).count) else: self.assertEqual(au_cities, self.get_names(qs.filter(point__dwithin=(self.au_pnt, dist)))) def test03a_distance_method(self): "Testing the `distance` GeoQuerySet method on projected coordinate systems." # The point for La Grange, TX lagrange = GEOSGeometry('POINT(-96.876369 29.905320)', 4326) # Reference distances in feet and in meters. Got these values from # using the provided raw SQL statements. # SELECT ST_Distance(point, ST_Transform(ST_GeomFromText('POINT(-96.876369 29.905320)', 4326), 32140)) FROM distapp_southtexascity; m_distances = [147075.069813, 139630.198056, 140888.552826, 138809.684197, 158309.246259, 212183.594374, 70870.188967, 165337.758878, 139196.085105] # SELECT ST_Distance(point, ST_Transform(ST_GeomFromText('POINT(-96.876369 29.905320)', 4326), 2278)) FROM distapp_southtexascityft; # Oracle 11 thinks this is not a projected coordinate system, so it's s # not tested. ft_distances = [482528.79154625, 458103.408123001, 462231.860397575, 455411.438904354, 519386.252102563, 696139.009211594, 232513.278304279, 542445.630586414, 456679.155883207] # Testing using different variations of parameters and using models # with different projected coordinate systems. dist1 = SouthTexasCity.objects.distance(lagrange, field_name='point') dist2 = SouthTexasCity.objects.distance(lagrange) # Using GEOSGeometry parameter if spatialite or oracle: dist_qs = [dist1, dist2] else: dist3 = SouthTexasCityFt.objects.distance(lagrange.ewkt) # Using EWKT string parameter. dist4 = SouthTexasCityFt.objects.distance(lagrange) dist_qs = [dist1, dist2, dist3, dist4] # Original query done on PostGIS, have to adjust AlmostEqual tolerance # for Oracle. if oracle: tol = 2 else: tol = 5 # Ensuring expected distances are returned for each distance queryset. for qs in dist_qs: for i, c in enumerate(qs): self.assertAlmostEqual(m_distances[i], c.distance.m, tol) self.assertAlmostEqual(ft_distances[i], c.distance.survey_ft, tol) @no_spatialite def test03b_distance_method(self): "Testing the `distance` GeoQuerySet method on geodetic coordnate systems." if oracle: tol = 2 else: tol = 5 # Testing geodetic distance calculation with a non-point geometry # (a LineString of Wollongong and Shellharbour coords). ls = LineString( ( (150.902, -34.4245), (150.87, -34.5789) ) ) if oracle or connection.ops.geography: # Reference query: # SELECT ST_distance_sphere(point, ST_GeomFromText('LINESTRING(150.9020 -34.4245,150.8700 -34.5789)', 4326)) FROM distapp_australiacity ORDER BY name; distances = [1120954.92533513, 140575.720018241, 640396.662906304, 60580.9693849269, 972807.955955075, 568451.8357838, 40435.4335201384, 0, 68272.3896586844, 12375.0643697706, 0] qs = AustraliaCity.objects.distance(ls).order_by('name') for city, distance in zip(qs, distances): # Testing equivalence to within a meter. self.assertAlmostEqual(distance, city.distance.m, 0) else: # PostGIS 1.4 and below is limited to disance queries only # to/from point geometries, check for raising of ValueError. self.assertRaises(ValueError, AustraliaCity.objects.distance, ls) self.assertRaises(ValueError, AustraliaCity.objects.distance, ls.wkt) # Got the reference distances using the raw SQL statements: # SELECT ST_distance_spheroid(point, ST_GeomFromText('POINT(151.231341 -33.952685)', 4326), 'SPHEROID["WGS 84",6378137.0,298.257223563]') FROM distapp_australiacity WHERE (NOT (id = 11)); # SELECT ST_distance_sphere(point, ST_GeomFromText('POINT(151.231341 -33.952685)', 4326)) FROM distapp_australiacity WHERE (NOT (id = 11)); st_distance_sphere if connection.ops.postgis and connection.ops.proj_version_tuple() >= (4, 7, 0): # PROJ.4 versions 4.7+ have updated datums, and thus different # distance values. spheroid_distances = [60504.0628957201, 77023.9489850262, 49154.8867574404, 90847.4358768573, 217402.811919332, 709599.234564757, 640011.483550888, 7772.00667991925, 1047861.78619339, 1165126.55236034] sphere_distances = [60580.9693849267, 77144.0435286473, 49199.4415344719, 90804.7533823494, 217713.384600405, 709134.127242793, 639828.157159169, 7786.82949717788, 1049204.06569028, 1162623.7238134] else: spheroid_distances = [60504.0628825298, 77023.948962654, 49154.8867507115, 90847.435881812, 217402.811862568, 709599.234619957, 640011.483583758, 7772.00667666425, 1047861.7859506, 1165126.55237647] sphere_distances = [60580.7612632291, 77143.7785056615, 49199.2725132184, 90804.4414289463, 217712.63666124, 709131.691061906, 639825.959074112, 7786.80274606706, 1049200.46122281, 1162619.7297006] # Testing with spheroid distances first. hillsdale = AustraliaCity.objects.get(name='Hillsdale') qs = AustraliaCity.objects.exclude(id=hillsdale.id).distance(hillsdale.point, spheroid=True) for i, c in enumerate(qs): self.assertAlmostEqual(spheroid_distances[i], c.distance.m, tol) if postgis: # PostGIS uses sphere-only distances by default, testing these as well. qs = AustraliaCity.objects.exclude(id=hillsdale.id).distance(hillsdale.point) for i, c in enumerate(qs): self.assertAlmostEqual(sphere_distances[i], c.distance.m, tol) @no_oracle # Oracle already handles geographic distance calculation. def test03c_distance_method(self): "Testing the `distance` GeoQuerySet method used with `transform` on a geographic field." # Normally you can't compute distances from a geometry field # that is not a PointField (on PostGIS 1.4 and below). if not connection.ops.geography: self.assertRaises(ValueError, CensusZipcode.objects.distance, self.stx_pnt) # We'll be using a Polygon (created by buffering the centroid # of 77005 to 100m) -- which aren't allowed in geographic distance # queries normally, however our field has been transformed to # a non-geographic system. z = SouthTexasZipcode.objects.get(name='77005') # Reference query: # SELECT ST_Distance(ST_Transform("distapp_censuszipcode"."poly", 32140), ST_GeomFromText('<buffer_wkt>', 32140)) FROM "distapp_censuszipcode"; dists_m = [3553.30384972258, 1243.18391525602, 2186.15439472242] # Having our buffer in the SRID of the transformation and of the field # -- should get the same results. The first buffer has no need for # transformation SQL because it is the same SRID as what was given # to `transform()`. The second buffer will need to be transformed, # however. buf1 = z.poly.centroid.buffer(100) buf2 = buf1.transform(4269, clone=True) ref_zips = ['77002', '77025', '77401'] for buf in [buf1, buf2]: qs = CensusZipcode.objects.exclude(name='77005').transform(32140).distance(buf) self.assertEqual(ref_zips, self.get_names(qs)) for i, z in enumerate(qs): self.assertAlmostEqual(z.distance.m, dists_m[i], 5) def test04_distance_lookups(self): "Testing the `distance_lt`, `distance_gt`, `distance_lte`, and `distance_gte` lookup types." # Retrieving the cities within a 20km 'donut' w/a 7km radius 'hole' # (thus, Houston and Southside place will be excluded as tested in # the `test02_dwithin` above). qs1 = SouthTexasCity.objects.filter(point__distance_gte=(self.stx_pnt, D(km=7))).filter(point__distance_lte=(self.stx_pnt, D(km=20))) # Can't determine the units on SpatiaLite from PROJ.4 string, and # Oracle 11 incorrectly thinks it is not projected. if spatialite or oracle: dist_qs = (qs1,) else: qs2 = SouthTexasCityFt.objects.filter(point__distance_gte=(self.stx_pnt, D(km=7))).filter(point__distance_lte=(self.stx_pnt, D(km=20))) dist_qs = (qs1, qs2) for qs in dist_qs: cities = self.get_names(qs) self.assertEqual(cities, ['Bellaire', 'Pearland', 'West University Place']) # Doing a distance query using Polygons instead of a Point. z = SouthTexasZipcode.objects.get(name='77005') qs = SouthTexasZipcode.objects.exclude(name='77005').filter(poly__distance_lte=(z.poly, D(m=275))) self.assertEqual(['77025', '77401'], self.get_names(qs)) # If we add a little more distance 77002 should be included. qs = SouthTexasZipcode.objects.exclude(name='77005').filter(poly__distance_lte=(z.poly, D(m=300))) self.assertEqual(['77002', '77025', '77401'], self.get_names(qs)) def test05_geodetic_distance_lookups(self): "Testing distance lookups on geodetic coordinate systems." # Line is from Canberra to Sydney. Query is for all other cities within # a 100km of that line (which should exclude only Hobart & Adelaide). line = GEOSGeometry('LINESTRING(144.9630 -37.8143,151.2607 -33.8870)', 4326) dist_qs = AustraliaCity.objects.filter(point__distance_lte=(line, D(km=100))) if oracle or connection.ops.geography: # Oracle and PostGIS 1.5 can do distance lookups on arbitrary geometries. self.assertEqual(9, dist_qs.count()) self.assertEqual(['Batemans Bay', 'Canberra', 'Hillsdale', 'Melbourne', 'Mittagong', 'Shellharbour', 'Sydney', 'Thirroul', 'Wollongong'], self.get_names(dist_qs)) else: # PostGIS 1.4 and below only allows geodetic distance queries (utilizing # ST_Distance_Sphere/ST_Distance_Spheroid) from Points to PointFields # on geometry columns. self.assertRaises(ValueError, dist_qs.count) # Ensured that a ValueError was raised, none of the rest of the test is # support on this backend, so bail now. if spatialite: return # Too many params (4 in this case) should raise a ValueError. self.assertRaises(ValueError, len, AustraliaCity.objects.filter(point__distance_lte=('POINT(5 23)', D(km=100), 'spheroid', '4'))) # Not enough params should raise a ValueError. self.assertRaises(ValueError, len, AustraliaCity.objects.filter(point__distance_lte=('POINT(5 23)',))) # Getting all cities w/in 550 miles of Hobart. hobart = AustraliaCity.objects.get(name='Hobart') qs = AustraliaCity.objects.exclude(name='Hobart').filter(point__distance_lte=(hobart.point, D(mi=550))) cities = self.get_names(qs) self.assertEqual(cities, ['Batemans Bay', 'Canberra', 'Melbourne']) # Cities that are either really close or really far from Wollongong -- # and using different units of distance. wollongong = AustraliaCity.objects.get(name='Wollongong') d1, d2 = D(yd=19500), D(nm=400) # Yards (~17km) & Nautical miles. # Normal geodetic distance lookup (uses `distance_sphere` on PostGIS. gq1 = Q(point__distance_lte=(wollongong.point, d1)) gq2 = Q(point__distance_gte=(wollongong.point, d2)) qs1 = AustraliaCity.objects.exclude(name='Wollongong').filter(gq1 | gq2) # Geodetic distance lookup but telling GeoDjango to use `distance_spheroid` # instead (we should get the same results b/c accuracy variance won't matter # in this test case). if postgis: gq3 = Q(point__distance_lte=(wollongong.point, d1, 'spheroid')) gq4 = Q(point__distance_gte=(wollongong.point, d2, 'spheroid')) qs2 = AustraliaCity.objects.exclude(name='Wollongong').filter(gq3 | gq4) querysets = [qs1, qs2] else: querysets = [qs1] for qs in querysets: cities = self.get_names(qs) self.assertEqual(cities, ['Adelaide', 'Hobart', 'Shellharbour', 'Thirroul']) def test06_area(self): "Testing the `area` GeoQuerySet method." # Reference queries: # SELECT ST_Area(poly) FROM distapp_southtexaszipcode; area_sq_m = [5437908.90234375, 10183031.4389648, 11254471.0073242, 9881708.91772461] # Tolerance has to be lower for Oracle and differences # with GEOS 3.0.0RC4 tol = 2 for i, z in enumerate(SouthTexasZipcode.objects.area()): self.assertAlmostEqual(area_sq_m[i], z.area.sq_m, tol) def test07_length(self): "Testing the `length` GeoQuerySet method." # Reference query (should use `length_spheroid`). # SELECT ST_length_spheroid(ST_GeomFromText('<wkt>', 4326) 'SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]]'); len_m1 = 473504.769553813 len_m2 = 4617.668 if spatialite: # Does not support geodetic coordinate systems. self.assertRaises(ValueError, Interstate.objects.length) else: qs = Interstate.objects.length() if oracle: tol = 2 else: tol = 5 self.assertAlmostEqual(len_m1, qs[0].length.m, tol) # Now doing length on a projected coordinate system. i10 = SouthTexasInterstate.objects.length().get(name='I-10') self.assertAlmostEqual(len_m2, i10.length.m, 2) @no_spatialite def test08_perimeter(self): "Testing the `perimeter` GeoQuerySet method." # Reference query: # SELECT ST_Perimeter(distapp_southtexaszipcode.poly) FROM distapp_southtexaszipcode; perim_m = [18404.3550889361, 15627.2108551001, 20632.5588368978, 17094.5996143697] if oracle: tol = 2 else: tol = 7 for i, z in enumerate(SouthTexasZipcode.objects.perimeter()): self.assertAlmostEqual(perim_m[i], z.perimeter.m, tol) # Running on points; should return 0. for i, c in enumerate(SouthTexasCity.objects.perimeter(model_att='perim')): self.assertEqual(0, c.perim.m) def test09_measurement_null_fields(self): "Testing the measurement GeoQuerySet methods on fields with NULL values." # Creating SouthTexasZipcode w/NULL value. SouthTexasZipcode.objects.create(name='78212') # Performing distance/area queries against the NULL PolygonField, # and ensuring the result of the operations is None. htown = SouthTexasCity.objects.get(name='Downtown Houston') z = SouthTexasZipcode.objects.distance(htown.point).area().get(name='78212') self.assertEqual(None, z.distance) self.assertEqual(None, z.area)
def deserialize(self, value): self.deserialize_called += 1 return GEOSGeometry(value)
def test_geodetic_distance_lookups(self): """ Test distance lookups on geodetic coordinate systems. """ # Line is from Canberra to Sydney. Query is for all other cities within # a 100km of that line (which should exclude only Hobart & Adelaide). line = GEOSGeometry('LINESTRING(144.9630 -37.8143,151.2607 -33.8870)', 4326) dist_qs = AustraliaCity.objects.filter(point__distance_lte=(line, D(km=100))) expected_cities = [ 'Batemans Bay', 'Canberra', 'Hillsdale', 'Melbourne', 'Mittagong', 'Shellharbour', 'Sydney', 'Thirroul', 'Wollongong', ] if spatialite: # SpatiaLite is less accurate and returns 102.8km for Batemans Bay. expected_cities.pop(0) self.assertEqual(expected_cities, self.get_names(dist_qs)) msg = "2, 3, or 4-element tuple required for 'distance_lte' lookup." with self.assertRaisesMessage(ValueError, msg): # Too many params. len( AustraliaCity.objects.filter( point__distance_lte=('POINT(5 23)', D(km=100), 'spheroid', '4', None))) with self.assertRaisesMessage(ValueError, msg): # Too few params. len( AustraliaCity.objects.filter( point__distance_lte=('POINT(5 23)', ))) msg = "For 4-element tuples the last argument must be the 'spheroid' directive." with self.assertRaisesMessage(ValueError, msg): len( AustraliaCity.objects.filter( point__distance_lte=('POINT(5 23)', D(km=100), 'spheroid', '4'))) # Getting all cities w/in 550 miles of Hobart. hobart = AustraliaCity.objects.get(name='Hobart') qs = AustraliaCity.objects.exclude(name='Hobart').filter( point__distance_lte=(hobart.point, D(mi=550))) cities = self.get_names(qs) self.assertEqual(cities, ['Batemans Bay', 'Canberra', 'Melbourne']) # Cities that are either really close or really far from Wollongong -- # and using different units of distance. wollongong = AustraliaCity.objects.get(name='Wollongong') d1, d2 = D(yd=19500), D(nm=400) # Yards (~17km) & Nautical miles. # Normal geodetic distance lookup (uses `distance_sphere` on PostGIS. gq1 = Q(point__distance_lte=(wollongong.point, d1)) gq2 = Q(point__distance_gte=(wollongong.point, d2)) qs1 = AustraliaCity.objects.exclude(name='Wollongong').filter(gq1 | gq2) # Geodetic distance lookup but telling GeoDjango to use `distance_spheroid` # instead (we should get the same results b/c accuracy variance won't matter # in this test case). querysets = [qs1] if connection.features.has_DistanceSpheroid_function: gq3 = Q(point__distance_lte=(wollongong.point, d1, 'spheroid')) gq4 = Q(point__distance_gte=(wollongong.point, d2, 'spheroid')) qs2 = AustraliaCity.objects.exclude( name='Wollongong').filter(gq3 | gq4) querysets.append(qs2) for qs in querysets: cities = self.get_names(qs) self.assertEqual( cities, ['Adelaide', 'Hobart', 'Shellharbour', 'Thirroul'])
def test_to_python_different_map_srid(self): f = forms.GeometryField(widget=OpenLayersWidget) json = '{ "type": "Point", "coordinates": [ 5.0, 23.0 ] }' self.assertEqual(GEOSGeometry('POINT(5 23)', srid=f.widget.map_srid), f.to_python(json))
def setUp(self): self.geometries = { 'point': GEOSGeometry("SRID=4326;POINT(9.052734375 42.451171875)"), }
def gen_bbox(): bbox_2d = GEOSGeometry(bbox_wkt, srid=32140) bbox_3d = Polygon(tuple( (x, y, z) for (x, y), z in zip(bbox_2d[0].coords, bbox_z)), srid=32140) return bbox_2d, bbox_3d
def geos_geometry(self, wkt): try: return GEOSGeometry(wkt) except: raise GEOSException
def save_geojson(self, tile, request, is_function_save_method=True): """Finds the equivalen GeoJSON for a BNG Alphanumeric value and saves that value to the geojson nodegroup of the tile. Args: self : BNGPointToGeoJSON object. tile : Tile to attach / amend geojson_nodegroup of. request : WSGI Request used to varify call is result of user action. N.B. Function Returns if empty. is_function_save_method : a bool stating whether the function calling it is the save function. """ # First let's check if this call is as a result of an inbound request (user action) or # as a result of the complementary GeoJSONToBNGPoint function saving a new BngPoint. if request is None and is_function_save_method == True: return bngValueReturned = "" gridSquare = { "NA": [0, 9], "NB": [1, 9], "NC": [2, 9], "ND": [3, 9], "NE": [4, 9], "OA": [5, 9], "OB": [6, 9], "NF": [0, 8], "NG": [1, 8], "NH": [2, 8], "NJ": [3, 8], "NK": [4, 8], "OF": [5, 8], "OG": [6, 8], "NL": [0, 7], "NM": [1, 7], "NN": [2, 7], "NO": [3, 7], "NP": [4, 7], "OL": [5, 7], "OM": [6, 7], "NQ": [0, 6], "NR": [1, 6], "NS": [2, 6], "NT": [3, 6], "NU": [4, 6], "OQ": [5, 6], "OR": [6, 6], "NV": [0, 5], "NW": [1, 5], "NX": [2, 5], "NY": [3, 5], "NZ": [4, 5], "OV": [5, 5], "OW": [6, 5], "SA": [0, 4], "SB": [1, 4], "SC": [2, 4], "SD": [3, 4], "SE": [4, 4], "TA": [5, 4], "TB": [6, 4], "SF": [0, 3], "SG": [1, 3], "SH": [2, 3], "SJ": [3, 3], "SK": [4, 3], "TF": [5, 3], "TG": [6, 3], "SL": [0, 2], "SM": [1, 2], "SN": [2, 2], "SO": [3, 2], "SP": [4, 2], "TL": [5, 2], "TM": [6, 2], "SQ": [0, 1], "SR": [1, 1], "SS": [2, 1], "ST": [3, 1], "SU": [4, 1], "TQ": [5, 1], "TR": [6, 1], "SV": [0, 0], "SW": [1, 0], "SX": [2, 0], "SY": [3, 0], "SZ": [4, 0], "TV": [5, 0], "TW": [6, 0], } bngnode = self.config["bng_node"] geojsonNode = self.config["geojson_node"] bngValueReturned = tile.data[bngnode] if bngValueReturned != None: """ The following section turns the alphanumberic BNG value in the tile into a point geometry object and then transforms that object into WGS 1984 long/lat projection system. """ dt = datetime.now() gridSquareLetters = bngValueReturned[0:2] bngValueNumbers = bngValueReturned[2:] splitSection = int(len(bngValueNumbers) / 2) gridSquareNumbers = gridSquare[gridSquareLetters] eastingValue = str(gridSquareNumbers[0]) + str(bngValueNumbers[:splitSection]) northingValue = str(gridSquareNumbers[1]) + str(bngValueNumbers[splitSection:]) osgb36PointString = "POINT (" + eastingValue + " " + northingValue + ")" osgb36Point = GEOSGeometry(osgb36PointString, srid=27700) osgb36Point.transform(4326, False) pointGeoJSON = json.loads(osgb36Point.geojson) """ This section creates a geojson object required in the format required by Arches. The date and time the object was created has also been added in the feature's properties. """ uuidForRecord = uuid.uuid4().hex bngFeature = { "geometry": pointGeoJSON, "type": "Feature", "id": str(uuidForRecord), "properties": {"datetime": dt.strftime("%d/%m/%Y %H:%M:%S"), "bngref": str(bngValueReturned)}, } geometryValue = {"type": "FeatureCollection", "features": [bngFeature]} geometryValueJson = geometryValue """ The Tile.objects.filter function from tiles.py is called to return any tiles with the geojson_nodegroup value as the nodegroup_id and the current tile's resource instance ID as its resourceinstance_id value; any tiles returned are added to the previously_saved_tiles variable. If there are tiles returned then the new geojson object overwrites the current value. If there are no tiles returned, a new tile is created for the geojson nodegroup using tile.py's get_blank_tile function. If there is a key within the data object in the new node with the same id as the geojson_nodegroup value then that key/value pair are deleted. The geojson object is set at the value to the key which has the value of the geojson_node value. The new tile is saved and then the mv_geojson_geoms materialised view is refreshed so the point geometry will be displayed on the Search map. """ if self.config["geojson_nodegroup"] == str(tile.nodegroup_id): tile.data[geojsonNode] = geometryValueJson else: previously_saved_tiles = Tile.objects.filter( nodegroup_id=self.config["geojson_nodegroup"], resourceinstance_id=tile.resourceinstance_id ) if len(previously_saved_tiles) > 0: for p in previously_saved_tiles: old_geojson = p.data[geojsonNode] p.data[geojsonNode] = geometryValueJson for f in old_geojson["features"]: if "bngref" not in f["properties"]: p.data[geojsonNode]["features"].append(f) p.save() else: new_geojson_tile = Tile().get_blank_tile_from_nodegroup_id( self.config["geojson_nodegroup"], resourceid=tile.resourceinstance_id, parenttile=tile.parenttile ) new_geojson_tile.data[self.config["geojson_node"]] = geometryValueJson if self.config["geojson_nodegroup"] in new_geojson_tile.data: del new_geojson_tile.data[self.config["geojson_nodegroup"]] new_geojson_tile.save() cursor = connection.cursor() sql = """ SELECT * FROM refresh_geojson_geometries(); """ cursor.execute(sql) # else: pass return
def get_context(self, name, value, attrs): # Update the template parameters with any attributes passed in. if attrs: self.params.update(attrs) self.params["editable"] = self.params["modifiable"] else: self.params["editable"] = True # Defaulting the WKT value to a blank string -- this # will be tested in the JavaScript and the appropriate # interface will be constructed. self.params["wkt"] = "" # If a string reaches here (via a validation error on another # field) then just reconstruct the Geometry. if value and isinstance(value, str): try: value = GEOSGeometry(value) except (GEOSException, ValueError) as err: logger.error("Error creating geometry from value '%s' (%s)", value, err) value = None if (value and value.geom_type.upper() != self.geom_type and self.geom_type != "GEOMETRY"): value = None # Constructing the dictionary of the map options. self.params["map_options"] = self.map_options() # Constructing the JavaScript module name using the name of # the GeometryField (passed in via the `attrs` keyword). # Use the 'name' attr for the field name (rather than 'field') self.params["name"] = name # note: we must switch out dashes for underscores since js # functions are created using the module variable js_safe_name = self.params["name"].replace("-", "_") self.params["module"] = "geodjango_%s" % js_safe_name if value: # Transforming the geometry to the projection used on the # OpenLayers map. srid = self.params["srid"] if value.srid != srid: try: ogr = value.ogr ogr.transform(srid) wkt = ogr.wkt except GDALException as err: logger.error( "Error transforming geometry from srid '%s' to srid '%s' (%s)", value.srid, srid, err, ) wkt = "" else: wkt = value.wkt # Setting the parameter WKT with that of the transformed # geometry. self.params["wkt"] = wkt self.params.update(geo_context) return self.params
def test_GeometryField_filtering(self): """ Checks that the GeometryField allows sane filtering. """ self.assertEqual(Location.objects.count(), 0) treasure_island_geojson = """{ "type": "Polygon", "coordinates": [ [ [ -122.44640350341795, 37.86103094116189 ], [ -122.44262695312501, 37.85506751416839 ], [ -122.43481636047363, 37.853305500228025 ], [ -122.42975234985352, 37.854660899304704 ], [ -122.41953849792479, 37.852627791344894 ], [ -122.41807937622069, 37.853305500228025 ], [ -122.41868019104004, 37.86211514878027 ], [ -122.42391586303711, 37.870584971740065 ], [ -122.43035316467285, 37.8723465726078 ], [ -122.43515968322752, 37.86963639998042 ], [ -122.43953704833984, 37.86882332875222 ], [ -122.44640350341795, 37.86103094116189 ] ] ] }""" treasure_island_geom = GEOSGeometry(treasure_island_geojson) treasure_island = Location() treasure_island.name = "Treasure Island" treasure_island.geometry = treasure_island_geom treasure_island.full_clean() treasure_island.save() ggpark_geojson = """{ "type": "Polygon", "coordinates": [ [ [ -122.5111198425293, 37.77125750792944 ], [ -122.51026153564452, 37.76447260365713 ], [ -122.45309829711913, 37.76677954095475 ], [ -122.45481491088867, 37.77424266859531 ], [ -122.5111198425293, 37.77125750792944 ] ] ] }""" ggpark_geom = GEOSGeometry(ggpark_geojson) ggpark = Location() ggpark.name = "Golden Gate Park" ggpark.geometry = ggpark_geom ggpark.save() point_inside_ggpark_geojson = """{ "type": "Point", "coordinates": [ -122.49034881591797, 37.76949349270407 ] }""" url_params = "?contains_properly=%s" % urllib.quote( point_inside_ggpark_geojson) response = self.client.get(self.geojson_contained_in_geometry + url_params) self.assertEqual(len(response.data), 1) geometry_response = GEOSGeometry( json.dumps(response.data[0]['geometry'])) self.assertTrue(geometry_response.equals_exact(ggpark_geom)) self.assertEqual(response.data[0]['name'], ggpark.name) # try without any param, should return both response = self.client.get(self.geojson_contained_in_geometry) self.assertEqual(len(response.data), 2)
def setUp(self): """Shared objects. Bugs with TestMommy vs django-polymorphic: https://github.com/django-polymorphic/django-polymorphic/issues/280 https://github.com/django-polymorphic/django-polymorphic/issues/109 polymorphic/models.py", line 112, in get_real_instance_class and not issubclass(model, self.__class__._meta.proxy_for_model): TypeError: issubclass() arg 2 must be a class or tuple of classes """ self.com0 = mommy.make(Community, code="code0", name="name0", _fill_optional=['eoo']) self.com0.save() self.com1 = mommy.make(Community, code="code1", name="name1", _fill_optional=['eoo']) self.com1.save() self.taxon0 = mommy.make(Taxon, name_id=1000, name="name0", _fill_optional=['rank', 'eoo']) self.taxon0.save() self.user = get_user_model().objects.create_superuser( username="******", email="*****@*****.**", password="******") self.user.save() self.consthreatcat = cons_models.ConservationThreatCategory.objects.create( code="weeds", label="Weeds", description="invasive weeds") self.consthreat = cons_models.ConservationThreat.objects.create( category=self.consthreatcat, cause="burnif of some stuff", occurrence_area_code="area1") self.consthreat.communities.add(self.com0) self.consactioncat = cons_models.ConservationActionCategory.objects.create( code="burn", label="Burn", description="Burn everything") self.consact = cons_models.ConservationAction.objects.create( category=self.consactioncat, instructions="burn some stuff", occurrence_area_code="area1") self.consact.communities.add(self.com0) self.cae = occ_models.CommunityAreaEncounter.objects.create( community=self.com0, encountered_on=timezone.now(), encountered_by=self.user, code="area1", label="Area 1", point=GEOSGeometry('POINT (115 -32)', srid=4326), geom=GEOSGeometry( '{ "type": "Polygon", "coordinates": [ [ [ 116.586914, -32.916485 ], ' '[ 116.586914, -30.977609 ], [ 120.27832, -30.977609 ], ' '[ 120.27832, -32.916485 ], [ 116.586914, -32.916485 ] ] ] }') ) self.cae1 = occ_models.CommunityAreaEncounter.objects.create( community=self.com0, encountered_on=timezone.now(), encountered_by=self.user, code="area2", label="Area 2", point=GEOSGeometry('POINT (115 -32)', srid=4326)) self.fatt = occ_models.FileAttachment.objects.create( encounter=self.cae, attachment=SimpleUploadedFile('testfile.txt', b'These are the file contents.'), title="test", author=self.user) self.fatt.save() self.fatt1 = occ_models.FileAttachment.objects.create( encounter=self.cae, attachment=SimpleUploadedFile('testfile.txt', b'These are the file contents.'), title="test", author=self.user, confidential=True, ) self.fatt1.save() self.asssp1 = occ_models.AssociatedSpecies.objects.create( encounter=self.cae, taxon=self.taxon0) self.asssp1.save() self.fh1 = occ_models.FireHistory.objects.create( encounter=self.cae, last_fire_date=timezone.now().date(), fire_intensity=occ_models.FireHistory.HMLN_HIGH) self.fh1.save() self.vc = occ_models.VegetationClassification.objects.create( encounter=self.cae, level1="lsdkfjlksdf", level2="slkdjflskdjf", level3="sdklsjdf", level4="sldkfjslkdjfskdf") self.vc.save() self.aa0 = occ_models.AreaAssessment.objects.create( encounter=self.cae, area_surveyed_m2=None, survey_duration_min=None) self.aa0.save() self.aa1 = occ_models.AreaAssessment.objects.create( encounter=self.cae, area_surveyed_m2=200, survey_duration_min=None) self.aa1.save() self.aa2 = occ_models.AreaAssessment.objects.create( encounter=self.cae, area_surveyed_m2=532, survey_duration_min=47) self.aa2.save() self.soiltype, created = occ_models.SoilType.objects.get_or_create( code="test-soil-type", label="Test soil type") self.hc = occ_models.HabitatComposition.objects.create( encounter=self.cae, # landform, # rock_type, # loose_rock_percent, soil_type=self.soiltype, # soil_colour, # drainage, ) self.habcond = occ_models.HabitatCondition.objects.get_or_create( encounter=self.cae, pristine_percent=12, excellent_percent=23, very_good_percent=34, good_percent=45, degraded_percent=56, completely_degraded_percent=67, ) # TODO: animalobservation, physicalsample, plantcount self.client.force_login(self.user)
def convert(self, value): from django.contrib.gis.geos import GEOSGeometry if value is None: return None return GEOSGeometry(value)
def setUp(self): super(MapAPITests, self).setUp() # Create the edit user and add it to the authenticated group self.edit_user = User(username="******", email="*****@*****.**", password="******") self.edit_user.save() all_group, created = Group.objects.get_or_create( name=settings.USERS_DEFAULT_GROUP) self.edit_user.groups.add(all_group) self.edit_user.save() self.edit_user_2 = User(username="******", email="*****@*****.**", password="******") self.edit_user_2.save() all_group, created = Group.objects.get_or_create( name=settings.USERS_DEFAULT_GROUP) self.edit_user_2.groups.add(all_group) self.edit_user_2.save() self.sf = Region(full_name="San Francisco", slug="sf") self.sf.save() self.oak = Region(full_name="Oakland", slug="oak") self.oak.save() self.golden_gate_park = Page(name="Golden Gate Park", content="<p>Hi</p>", region=self.sf) self.golden_gate_park.save() self.dolores_park = Page(name="Dolores Park", content="<p>Hi</p>", region=self.sf) self.dolores_park.save() self.duboce_park = Page(name="Duboce Park", content="<p>Hi</p>", region=self.sf) self.duboce_park.save() self.dolores_park_map = MapData(page=self.dolores_park, region=self.sf) self.dolores_park_map.geom = GEOSGeometry(""" { "type": "GeometryCollection", "geometries": [ { "type": "Polygon", "coordinates": [ [ [ -122.42835760116576, 37.76128348360843 ], [ -122.42799282073973, 37.75812815505155 ], [ -122.42591142654419, 37.758297799795336 ], [ -122.42627620697021, 37.761402229904654 ], [ -122.42835760116576, 37.76128348360843 ] ] ] }] }""") self.dolores_park_map.save() self.duboce_park_map = MapData(page=self.duboce_park, region=self.sf) self.duboce_park_map.geom = GEOSGeometry(""" { "type": "GeometryCollection", "geometries": [ { "type": "Polygon", "coordinates": [ [ [ -122.43505239486696, 37.770443352285376 ], [ -122.43490219116211, 37.76917121614543 ], [ -122.431640625, 37.769408683219545 ], [ -122.43179082870485, 37.76991753866766 ], [ -122.43460178375243, 37.769713996908635 ], [ -122.43505239486696, 37.770443352285376 ] ] ] } ] } """) self.duboce_park_map.save()
def test_node_geometry_distance_and_area(self): """ test minimum distance check between nodes """ self.client.login(username='******', password='******') url = reverse('api_node_list') json_data = { "layer": "rome", "name": "test_distance", "slug": "test_distance", "address": "via dei test", "description": "", "geometry": json.loads( GEOSGeometry( "POINT (12.5822391919000012 41.8720419276999820)").json), } layer = Layer.objects.get(pk=1) layer.nodes_minimum_distance = 100 layer.full_clean() layer.save() # Node coordinates don't respect minimum distance. Insert should fail because coords are near to already existing PoI ( fusolab ) response = self.client.post(url, json.dumps(json_data), content_type='application/json') self.assertEqual(400, response.status_code) # Node coordinates respect minimum distance. Insert should succed json_data['geometry'] = json.loads( GEOSGeometry("POINT (12.7822391919 41.8720419277)").json) response = self.client.post(url, json.dumps(json_data), content_type='application/json') self.assertEqual(201, response.status_code) # Disable minimum distance control in layer and update node inserting coords too near. Insert should succed layer.nodes_minimum_distance = 0 layer.save() json_data['geometry'] = json.loads( GEOSGeometry("POINT (12.5822391917 41.872042278)").json) n = Node.objects.get(slug='test_distance') node_slug = n.slug url = reverse('api_node_details', args=[node_slug]) response = self.client.put(url, json.dumps(json_data), content_type='application/json') self.assertEqual(200, response.status_code) # re-enable minimum distance and update again with coords too near. Insert should fail layer.nodes_minimum_distance = 100 layer.save() url = reverse('api_node_details', args=[node_slug]) response = self.client.put(url, json.dumps(json_data), content_type='application/json') self.assertEqual(400, response.status_code) # Defining an area for the layer and testing if node is inside the area layer.area = GEOSGeometry( 'POLYGON ((12.19 41.92, 12.58 42.17, 12.82 41.86, 12.43 41.64, 12.43 41.65, 12.19 41.92))' ) layer.save() # Node update should fail because coords are outside layer area json_data['geometry'] = json.loads(GEOSGeometry("POINT (50 50)").json) url = reverse('api_node_details', args=[node_slug]) response = self.client.put(url, json.dumps(json_data), content_type='application/json') self.assertEqual(400, response.status_code) # Node update should succeed because coords are inside layer area and respect minimum distance json_data['geometry'] = json.loads( GEOSGeometry("POINT (12.7822391919 41.8720419277)").json) url = reverse('api_node_details', args=[node_slug]) response = self.client.put(url, json.dumps(json_data), content_type='application/json') self.assertEqual(200, response.status_code) # Node update should succeed because layer area is disabled layer.area = GEOSGeometry("POINT (12.7822391919 41.8720419277)") layer.save() json_data['geometry'] = json.loads(GEOSGeometry("POINT (50 50)").json) url = reverse('api_node_details', args=[node_slug]) response = self.client.put(url, json.dumps(json_data), content_type='application/json') self.assertEqual(200, response.status_code) # re-enable minimum distance layer.nodes_minimum_distance = 100 layer.save() # delete new nodes just added before n.delete()
def test05_geodetic_distance_lookups(self): "Testing distance lookups on geodetic coordinate systems." # Line is from Canberra to Sydney. Query is for all other cities within # a 100km of that line (which should exclude only Hobart & Adelaide). line = GEOSGeometry('LINESTRING(144.9630 -37.8143,151.2607 -33.8870)', 4326) dist_qs = AustraliaCity.objects.filter(point__distance_lte=(line, D(km=100))) if oracle or connection.ops.geography: # Oracle and PostGIS 1.5 can do distance lookups on arbitrary geometries. self.assertEqual(9, dist_qs.count()) self.assertEqual(['Batemans Bay', 'Canberra', 'Hillsdale', 'Melbourne', 'Mittagong', 'Shellharbour', 'Sydney', 'Thirroul', 'Wollongong'], self.get_names(dist_qs)) else: # PostGIS 1.4 and below only allows geodetic distance queries (utilizing # ST_Distance_Sphere/ST_Distance_Spheroid) from Points to PointFields # on geometry columns. self.assertRaises(ValueError, dist_qs.count) # Ensured that a ValueError was raised, none of the rest of the test is # support on this backend, so bail now. if spatialite: return # Too many params (4 in this case) should raise a ValueError. self.assertRaises(ValueError, len, AustraliaCity.objects.filter(point__distance_lte=('POINT(5 23)', D(km=100), 'spheroid', '4'))) # Not enough params should raise a ValueError. self.assertRaises(ValueError, len, AustraliaCity.objects.filter(point__distance_lte=('POINT(5 23)',))) # Getting all cities w/in 550 miles of Hobart. hobart = AustraliaCity.objects.get(name='Hobart') qs = AustraliaCity.objects.exclude(name='Hobart').filter(point__distance_lte=(hobart.point, D(mi=550))) cities = self.get_names(qs) self.assertEqual(cities, ['Batemans Bay', 'Canberra', 'Melbourne']) # Cities that are either really close or really far from Wollongong -- # and using different units of distance. wollongong = AustraliaCity.objects.get(name='Wollongong') d1, d2 = D(yd=19500), D(nm=400) # Yards (~17km) & Nautical miles. # Normal geodetic distance lookup (uses `distance_sphere` on PostGIS. gq1 = Q(point__distance_lte=(wollongong.point, d1)) gq2 = Q(point__distance_gte=(wollongong.point, d2)) qs1 = AustraliaCity.objects.exclude(name='Wollongong').filter(gq1 | gq2) # Geodetic distance lookup but telling GeoDjango to use `distance_spheroid` # instead (we should get the same results b/c accuracy variance won't matter # in this test case). if postgis: gq3 = Q(point__distance_lte=(wollongong.point, d1, 'spheroid')) gq4 = Q(point__distance_gte=(wollongong.point, d2, 'spheroid')) qs2 = AustraliaCity.objects.exclude(name='Wollongong').filter(gq3 | gq4) querysets = [qs1, qs2] else: querysets = [qs1] for qs in querysets: cities = self.get_names(qs) self.assertEqual(cities, ['Adelaide', 'Hobart', 'Shellharbour', 'Thirroul'])
def test_all_gis_lookups_with_rasters(self): """ Evaluate all possible lookups for all input combinations (i.e. raster-raster, raster-geom, geom-raster) and for projected and unprojected coordinate systems. This test just checks that the lookup can be called, but doesn't check if the result makes logical sense. """ from django.contrib.gis.db.backends.postgis.operations import PostGISOperations # Create test raster and geom. rast = GDALRaster(json.loads(JSON_RASTER)) stx_pnt = GEOSGeometry( 'POINT (-95.370401017314293 29.704867409475465)', 4326) stx_pnt.transform(3086) # Loop through all the GIS lookups. for name, lookup in BaseSpatialField.get_lookups().items(): if not isinstance(lookup, GISLookup): continue # Construct lookup filter strings. combo_keys = [ field + name for field in [ 'rast__', 'rast__', 'rastprojected__0__', 'rast__', 'rastprojected__', 'geom__', 'rast__', ] ] if issubclass(lookup, DistanceLookupBase): # Set lookup values for distance lookups. combo_values = [ (rast, 50, 'spheroid'), (rast, 0, 50, 'spheroid'), (rast, 0, D(km=1)), (stx_pnt, 0, 500), (stx_pnt, D(km=1000)), (rast, 500), (json.loads(JSON_RASTER), 500), ] elif name == 'relate': # Set lookup values for the relate lookup. combo_values = [ (rast, 'T*T***FF*'), (rast, 0, 'T*T***FF*'), (rast, 0, 'T*T***FF*'), (stx_pnt, 0, 'T*T***FF*'), (stx_pnt, 'T*T***FF*'), (rast, 'T*T***FF*'), (json.loads(JSON_RASTER), 'T*T***FF*'), ] elif name == 'isvalid': # The isvalid lookup doesn't make sense for rasters. continue elif PostGISOperations.gis_operators[name].func: # Set lookup values for all function based operators. combo_values = [ rast, (rast, 0), (rast, 0), (stx_pnt, 0), stx_pnt, rast, rast, json.loads(JSON_RASTER) ] else: # Override band lookup for these, as it's not supported. combo_keys[2] = 'rastprojected__' + name # Set lookup values for all other operators. combo_values = [ rast, rast, rast, stx_pnt, stx_pnt, rast, rast, json.loads(JSON_RASTER) ] # Create query filter combinations. combos = [{x[0]: x[1]} for x in zip(combo_keys, combo_values)] for combo in combos: # Apply this query filter. qs = RasterModel.objects.filter(**combo) # Evaluate normal filter qs. self.assertIn(qs.count(), [0, 1]) # Evaluate on conditional Q expressions. qs = RasterModel.objects.filter(Q(**combos[0]) & Q(**combos[1])) self.assertIn(qs.count(), [0, 1])
try: from django.contrib.gis.geos import GEOSGeometry except (ImportError, Exception): """GDAL / GEOS not installed. Tests will fail if contrib.gis is installed, and will be skipped otherwise""" GEOSGeometry = None # noqa import floppyforms as forms # Some test data, geometries as OpenLayers serializes them. # GEOS's WKT reader normalizes them. # This is a callable to prevent us from getting warnings from the GEOS C api GEOMETRIES = lambda: { 'point': GEOSGeometry("""SRID=4326;POINT(9.052734375 42.451171875)"""), 'multipoint': GEOSGeometry("SRID=4326;MULTIPOINT(" "(13.18634033203125 14.504356384277344)," "(13.207969665527 14.490966796875)," "(13.177070617675 14.454917907714))"), 'linestring': GEOSGeometry("SRID=4326;LINESTRING(" "-8.26171875 -0.52734375," "-7.734375 4.21875," "6.85546875 3.779296875," "5.44921875 -3.515625)"), 'multilinestring': GEOSGeometry("SRID=4326;MULTILINESTRING(" "(-16.435546875 -2.98828125," "-17.2265625 2.98828125,"
def test_dwithin_gis_lookup_ouptut_with_rasters(self): """ Check the logical functionality of the dwithin lookup for different input parameters. """ # Create test raster and geom. rast = GDALRaster(json.loads(JSON_RASTER)) stx_pnt = GEOSGeometry( 'POINT (-95.370401017314293 29.704867409475465)', 4326) stx_pnt.transform(3086) # Filter raster with different lookup raster formats. qs = RasterModel.objects.filter(rastprojected__dwithin=(rast, D(km=1))) self.assertEqual(qs.count(), 1) qs = RasterModel.objects.filter( rastprojected__dwithin=(json.loads(JSON_RASTER), D(km=1))) self.assertEqual(qs.count(), 1) qs = RasterModel.objects.filter(rastprojected__dwithin=(JSON_RASTER, D(km=1))) self.assertEqual(qs.count(), 1) # Filter in an unprojected coordinate system. qs = RasterModel.objects.filter(rast__dwithin=(rast, 40)) self.assertEqual(qs.count(), 1) # Filter with band index transform. qs = RasterModel.objects.filter(rast__1__dwithin=(rast, 1, 40)) self.assertEqual(qs.count(), 1) qs = RasterModel.objects.filter(rast__1__dwithin=(rast, 40)) self.assertEqual(qs.count(), 1) qs = RasterModel.objects.filter(rast__dwithin=(rast, 1, 40)) self.assertEqual(qs.count(), 1) # Filter raster by geom. qs = RasterModel.objects.filter(rast__dwithin=(stx_pnt, 500)) self.assertEqual(qs.count(), 1) qs = RasterModel.objects.filter(rastprojected__dwithin=(stx_pnt, D(km=10000))) self.assertEqual(qs.count(), 1) qs = RasterModel.objects.filter(rast__dwithin=(stx_pnt, 5)) self.assertEqual(qs.count(), 0) qs = RasterModel.objects.filter(rastprojected__dwithin=(stx_pnt, D(km=100))) self.assertEqual(qs.count(), 0) # Filter geom by raster. qs = RasterModel.objects.filter(geom__dwithin=(rast, 500)) self.assertEqual(qs.count(), 1) # Filter through related model. qs = RasterRelatedModel.objects.filter( rastermodel__rast__dwithin=(rast, 40)) self.assertEqual(qs.count(), 1) # Filter through related model with band index transform qs = RasterRelatedModel.objects.filter( rastermodel__rast__1__dwithin=(rast, 40)) self.assertEqual(qs.count(), 1) # Filter through conditional statements. qs = RasterModel.objects.filter( Q(rast__dwithin=(rast, 40)) & Q(rastprojected__dwithin=(stx_pnt, D(km=10000)))) self.assertEqual(qs.count(), 1) # Filter through different lookup. qs = RasterModel.objects.filter(rastprojected__bbcontains=rast) self.assertEqual(qs.count(), 1)
def setUp(self): self.path = os.path.dirname(os.path.realpath(__file__)) self.group, created = Group.objects.get_or_create( name="TestDefaultExportExtentGroup") self.user1 = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.user2 = User.objects.create_user(username="******", email="*****@*****.**", password="******") extents = (-3.9, 16.1, 7.0, 27.6) bbox = Polygon.from_bbox(extents) original_selection = GeometryCollection( Point(1, 1), LineString((5.625, 48.458), (0.878, 44.339))) the_geom = GEOSGeometry(bbox, srid=4326) self.job = Job.objects.create( name="TestJob", event="Test Activation", description="Test description", user=self.user1, the_geom=the_geom, original_selection=original_selection, ) formats = ExportFormat.objects.all() provider = DataProvider.objects.first() provider_task = DataProviderTask.objects.create(provider=provider) provider_task.formats.add(*formats) self.job.data_provider_tasks.add(provider_task) token = Token.objects.create(user=self.user1) self.client.credentials( HTTP_AUTHORIZATION="Token " + token.key, HTTP_ACCEPT="application/json; version=1.0", HTTP_ACCEPT_LANGUAGE="en", HTTP_HOST="testserver", ) # create a test config hdm_presets = DatamodelPreset.objects.get(name="hdm") self.job.preset = hdm_presets self.job.save() group1, created = Group.objects.get_or_create(name="group_one") self.group1id = group1.id GroupPermission.objects.create( group=group1, user=self.user1, permission=GroupPermissionLevel.ADMIN.value) GroupPermission.objects.create( group=group1, user=self.user2, permission=GroupPermissionLevel.MEMBER.value) group2, created = Group.objects.get_or_create(name="group_two") self.group2id = group2.id GroupPermission.objects.create( group=group2, user=self.user1, permission=GroupPermissionLevel.ADMIN.value) GroupPermission.objects.create( group=group2, user=self.user2, permission=GroupPermissionLevel.MEMBER.value)
def update_geo_target_result(geo_target_id): """ Update in progress GeoTarget with result from BSD and set as complete Parameters ---------- geo_target_id : int GeoTarget id Returns ------- int Returns updated GeoTarget status """ """Get GeoTarget""" geo_target = GeoTarget.objects.get(id=geo_target_id) """Status should be new, otherwise do nothing""" if geo_target.status != GeoTargetStatus.new.value[0]: return geo_target.status """Set to in progress and start building result""" geo_target.status = GeoTargetStatus.in_progress.value[0] geo_target.save() try: """Get shape from geo json""" """Logic copied over from hydra app""" gjson = json.loads(geo_target.geo_json) if gjson['type'] == 'FeatureCollection': # todo: fetch number, but stick to 1st for now gjson = gjson['features'][0]['geometry'] geo_shape = GEOSGeometry(json.dumps(gjson)) except (GEOSException, GDALException, ValueError): geo_target.result = '''The geo json is invalid. Please double check the geo json and try again with a new geo target request. '''.replace('\n', '') geo_target.status = GeoTargetStatus.no_results.value[0] geo_target.save() return geo_target.status """ Get constitents by state first and we will trim it down later. Don't filter by subscribers only. """ constituents = find_constituents_by_state_cd( geo_target.state_or_territory, cons_group=None, subscribers_only=False, primary_address_only=geo_target.primary_address_only, with_email=False, with_phone=False, ) """Update status and exit if we did not find any constituents""" if constituents is None: geo_target.status = GeoTargetStatus.no_results.value[0] geo_target.save() return geo_target.status """Loop through constituents and update result""" result = "" result_count = 0 for constituent in constituents: constituent_id = constituent.get('id') """Get constituent location""" constituent_addresses = constituent.findall('cons_addr') for constituent_address in constituent_addresses: constituent_latitude = constituent_address.findtext('latitude') constituent_longitude = constituent_address.findtext('longitude') if constituent_latitude is not None and ( constituent_longitude is not None ): constituent_point = Point( y=float(constituent_latitude), x=float(constituent_longitude) ) """ Add to result if point is in shape and go to next constituent """ if geo_shape.contains(constituent_point): result += "%s, " % constituent_id result_count += 1 break """Update result and status""" geo_target.result = result geo_target.result_count = result_count geo_target.status = GeoTargetStatus.complete.value[0] geo_target.save() """Return updated GeoTarget status""" return geo_target.status
#if(len(new.dirTag) < 1): # print new.vid + " route "+new.routeTag+" has no dirTag" # continue new.t = t - int(vehicle.getAttribute('secsSinceReport')) if vehicle.getAttribute('predictable') == 'true': new.predictable = True else: new.predictable = False new.heading = int(vehicle.getAttribute('heading')) new.speedKmHr = float(vehicle.getAttribute('speedKmHr')) new.speedkmhr = float(vehicle.getAttribute('speedKmHr')) new.lat = float(vehicle.getAttribute('lat')) new.lon = float(vehicle.getAttribute('lon')) new.latlon = GEOSGeometry("POINT({0} {1})".format(new.lon, new.lat), srid=4326) # if(isInYard(new)): # continue p = getNearestPath(new) s = getNearestStreet(new) print new.vid, new.heading, s try: new.save() except Exception as e: print e continue if (vehicle.getAttribute('leadingVehicleID')): new.leadingVehicleId = int( vehicle.getAttribute('leadingVehicleID')) else:
def add_polygon(geom_dict): geom = GEOSGeometry(json.dumps(geom_dict), 4326) geom.transform(web_mercator) geoms.append(geom)
class ProjectFactory(factory.django.DjangoModelFactory): class Meta: model = Project name = factory.Sequence(lambda n: 'project %d' % n) description = factory.LazyAttribute(lambda o: '%s description' % o.name) isprivate = True islocked = False everyone_contributes = 'false' created_at = datetime.date(2014, 11, 11) creator = factory.SubFactory(UserFactory) status = 'active' geographic_extent = GEOSGeometry( '{"type": "Polygon","coordinates": [[[-0.508,51.682],[-0.53,51.327],' '[0.225,51.323],[0.167,51.667],[-0.508,51.682]]]}') @factory.post_generation def add_admins(self, create, extracted, **kwargs): if not create: return Admins.objects.create(project=self, user=self.creator) if extracted: for user in extracted: Admins.objects.create(project=self, user=user) @factory.post_generation def add_moderators(self, create, extracted, **kwargs): if not create: return if extracted: UserGroupFactory(add_users=extracted, **{ 'project': self, 'can_moderate': True }) @factory.post_generation def add_contributors(self, create, extracted, **kwargs): if not create: return if extracted: UserGroupFactory(add_users=extracted, **{ 'project': self, 'can_contribute': True }) @factory.post_generation def add_viewer(self, create, extracted, **kwargs): if not create: return if extracted: UserGroupFactory(add_users=extracted, **{ 'project': self, 'can_contribute': False, 'can_moderate': False, })
def test_geojson_sync(self): """ test GeoJSON sync """ layer = Layer.objects.external()[0] layer.new_nodes_allowed = False layer.save() layer = Layer.objects.get(pk=layer.pk) external = LayerExternal(layer=layer) external.synchronizer_path = 'nodeshot.interop.sync.synchronizers.GeoJson' external._reload_schema() external.url = '%s/geojson1.json' % self.STATIC_TEST_URL external.full_clean() external.save() output = capture_output(management.call_command, ['sync', 'vienna'], kwargs={'verbosity': 0}) # ensure following text is in output self.assertIn('2 nodes added', output) self.assertIn('0 nodes changed', output) self.assertIn('2 total external', output) self.assertIn('2 total local', output) # ensure all nodes have been imported self.assertEqual(layer.node_set.count(), 2) # check one particular node has the data we expect it to have node = Node.objects.get(slug='simplegeojson') self.assertEqual(node.name, 'simplegeojson') self.assertIn('simplegeojson', node.address) geometry = GEOSGeometry( '{"type":"Polygon","coordinates":[[[12.501493164066,41.990441051094],[12.583890625003,41.957770034531],[12.618222900394,41.912820024702],[12.607923217778,41.877552973685],[12.582088180546,41.82423212474],[12.574148841861,41.813357913568],[12.551532455447,41.799730560554],[12.525053688052,41.795155470656],[12.510505386356,41.793715689492],[12.43308610535,41.803249638226],[12.388883300784,41.813613798573],[12.371030517581,41.870906276755],[12.382016845706,41.898511105474],[12.386136718753,41.912820024702],[12.38064355469,41.926104006681],[12.38064355469,41.955727539561],[12.413602539065,41.974107637675],[12.445188232426,41.983295698272],[12.45617456055,41.981254021593],[12.476773925785,41.985337309484],[12.490506835941,41.985337309484],[12.506986328129,41.990441051094],[12.501493164066,41.990441051094]]]}' ) self.assertTrue( node.geometry.equals_exact(geometry) or node.geometry.equals(geometry)) self.assertEqual(node.elev, 10.0) # --- repeat --- # output = capture_output(management.call_command, ['sync', 'vienna'], kwargs={'verbosity': 0}) # ensure following text is in output self.assertIn('2 nodes unmodified', output) self.assertIn('0 nodes deleted', output) self.assertIn('0 nodes changed', output) self.assertIn('2 total external', output) self.assertIn('2 total local', output) # --- repeat with slightly different input --- # external.url = '%s/geojson2.json' % self.STATIC_TEST_URL external.full_clean() external.save() output = capture_output(management.call_command, ['sync', 'vienna'], kwargs={'verbosity': 0}) # ensure following text is in output self.assertIn('1 nodes unmodified', output) self.assertIn('0 nodes deleted', output) self.assertIn('1 nodes changed', output) self.assertIn('2 total external', output) self.assertIn('2 total local', output)