def test_simplify_geojson(self): fn = functions.AsGeoJSON(query.Simplify( functions.Transform('geom', self.srid), self.tol), precision=2) sqs = self.qs.all().annotate(geojson=fn) geom = geos.GEOSGeometry(sqs[0].geojson, self.srid) source = self.qs[0].geom self.assertNotEqual(geom, source) self.assertNotEqual(geom.srid, source.srid) self.assertLess(geom.num_coords, source.num_coords)
def test_asgeojson(self): # Only PostGIS and SpatiaLite support GeoJSON. if not connection.ops.geojson: with self.assertRaises(NotImplementedError): list( Country.objects.annotate( json=functions.AsGeoJSON('mpoly'))) return pueblo_json = '{"type":"Point","coordinates":[-104.609252,38.255001]}' houston_json = ( '{"type":"Point","crs":{"type":"name","properties":' '{"name":"EPSG:4326"}},"coordinates":[-95.363151,29.763374]}') victoria_json = ( '{"type":"Point","bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],' '"coordinates":[-123.305196,48.462611]}') chicago_json = ( '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},' '"bbox":[-87.65018,41.85039,-87.65018,41.85039],"coordinates":[-87.65018,41.85039]}' ) # Precision argument should only be an integer with self.assertRaises(TypeError): City.objects.annotate( geojson=functions.AsGeoJSON('point', precision='foo')) # Reference queries and values. # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 0) # FROM "geoapp_city" WHERE "geoapp_city"."name" = 'Pueblo'; self.assertJSONEqual( pueblo_json, City.objects.annotate(geojson=functions.AsGeoJSON('point')).get( name='Pueblo').geojson) # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 2) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Houston'; # This time we want to include the CRS by using the `crs` keyword. self.assertJSONEqual( houston_json, City.objects.annotate( json=functions.AsGeoJSON('point', crs=True)).get( name='Houston').json) # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 1) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Houston'; # This time we include the bounding box by using the `bbox` keyword. self.assertJSONEqual( victoria_json, City.objects.annotate( geojson=functions.AsGeoJSON('point', bbox=True)).get( name='Victoria').geojson) # SELECT ST_AsGeoJson("geoapp_city"."point", 5, 3) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Chicago'; # Finally, we set every available keyword. self.assertJSONEqual( chicago_json, City.objects.annotate(geojson=functions.AsGeoJSON( 'point', bbox=True, crs=True, precision=5)).get( name='Chicago').geojson)
def test_geometry_field_source(self): response = self.client.get('/locations/', {'format': 'geojson', 'page': 1}) context = {k: v for k, v in response.renderer_context.items() if k in ('request', 'view')} serializer = LocationFeatureSerializer( Location.objects.annotate(geojson=functions.AsGeoJSON('geom')), many=True, context=context) fields = serializer.child.fields self.assertEqual(fields['geom'].source, 'geojson') # Test serializing paginated objects. page = response.renderer_context['view'].paginator.page serializer = LocationFeatureSerializer( page, many=True, context=context) fields = serializer.child.fields self.assertEqual(fields['geom'].source, 'geojson')
def test_geojson_response(self): gj = self.qs.annotate(geojson=sqlfn.AsGeoJSON( 'geom', precision=self.precision))[0].geojson expected = json.loads(gj) with self.assertNumQueries(1): response = self.client.get(self.url, { 'format': 'geojson', 'precision': self.precision }) self.assertEqual(response.status_code, 200) feature = response.json() self.assertEqual(feature['geometry'], expected) self.assertEqual(feature['type'], 'Feature') # Be sure we get geojson returned from SQL function call, not GEOS. sqlformat = '{"type":"Polygon","coordinates":[[[' self.assertContains(response, 'geometry": %s' % sqlformat)
def test_asgeojson(self): if not connection.features.has_AsGeoJSON_function: with self.assertRaises(NotSupportedError): list( Country.objects.annotate( json=functions.AsGeoJSON("mpoly"))) return pueblo_json = '{"type":"Point","coordinates":[-104.609252,38.255001]}' houston_json = json.loads( '{"type":"Point","crs":{"type":"name","properties":' '{"name":"EPSG:4326"}},"coordinates":[-95.363151,29.763374]}') victoria_json = json.loads( '{"type":"Point",' '"bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],' '"coordinates":[-123.305196,48.462611]}') chicago_json = json.loads( '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},' '"bbox":[-87.65018,41.85039,-87.65018,41.85039],' '"coordinates":[-87.65018,41.85039]}') if "crs" in connection.features.unsupported_geojson_options: del houston_json["crs"] del chicago_json["crs"] if "bbox" in connection.features.unsupported_geojson_options: del chicago_json["bbox"] del victoria_json["bbox"] if "precision" in connection.features.unsupported_geojson_options: chicago_json["coordinates"] = [-87.650175, 41.850385] # Precision argument should only be an integer with self.assertRaises(TypeError): City.objects.annotate( geojson=functions.AsGeoJSON("point", precision="foo")) # Reference queries and values. # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 0) # FROM "geoapp_city" WHERE "geoapp_city"."name" = 'Pueblo'; self.assertJSONEqual( pueblo_json, City.objects.annotate(geojson=functions.AsGeoJSON("point")).get( name="Pueblo").geojson, ) # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 2) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Houston'; # This time we want to include the CRS by using the `crs` keyword. self.assertJSONEqual( City.objects.annotate( json=functions.AsGeoJSON("point", crs=True)).get( name="Houston").json, houston_json, ) # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 1) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Houston'; # This time we include the bounding box by using the `bbox` keyword. self.assertJSONEqual( City.objects.annotate( geojson=functions.AsGeoJSON("point", bbox=True)).get( name="Victoria").geojson, victoria_json, ) # SELECT ST_AsGeoJson("geoapp_city"."point", 5, 3) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Chicago'; # Finally, we set every available keyword. # MariaDB doesn't limit the number of decimals in bbox. if connection.ops.mariadb: chicago_json["bbox"] = [ -87.650175, 41.850385, -87.650175, 41.850385 ] try: self.assertJSONEqual( City.objects.annotate(geojson=functions.AsGeoJSON( "point", bbox=True, crs=True, precision=5)).get( name="Chicago").geojson, chicago_json, ) except AssertionError: # Give a second chance with different coords rounding. chicago_json["coordinates"][1] = 41.85038 self.assertJSONEqual( City.objects.annotate(geojson=functions.AsGeoJSON( "point", bbox=True, crs=True, precision=5)).get( name="Chicago").geojson, chicago_json, )
def test_asgeojson(self): if not connection.features.has_AsGeoJSON_function: with self.assertRaises(NotSupportedError): list( Country.objects.annotate( json=functions.AsGeoJSON('mpoly'))) return pueblo_json = '{"type":"Point","coordinates":[-104.609252,38.255001]}' houston_json = json.loads( '{"type":"Point","crs":{"type":"name","properties":' '{"name":"EPSG:4326"}},"coordinates":[-95.363151,29.763374]}') victoria_json = json.loads( '{"type":"Point","bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],' '"coordinates":[-123.305196,48.462611]}') chicago_json = json.loads( '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},' '"bbox":[-87.65018,41.85039,-87.65018,41.85039],"coordinates":[-87.65018,41.85039]}' ) # MySQL and Oracle ignore the crs option. if mysql or oracle: del houston_json['crs'] del chicago_json['crs'] # Oracle ignores also the bbox and precision options. if oracle: del chicago_json['bbox'] del victoria_json['bbox'] chicago_json['coordinates'] = [-87.650175, 41.850385] # Precision argument should only be an integer with self.assertRaises(TypeError): City.objects.annotate( geojson=functions.AsGeoJSON('point', precision='foo')) # Reference queries and values. # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 0) # FROM "geoapp_city" WHERE "geoapp_city"."name" = 'Pueblo'; self.assertJSONEqual( pueblo_json, City.objects.annotate(geojson=functions.AsGeoJSON('point')).get( name='Pueblo').geojson) # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 2) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Houston'; # This time we want to include the CRS by using the `crs` keyword. self.assertJSONEqual( City.objects.annotate( json=functions.AsGeoJSON('point', crs=True)).get( name='Houston').json, houston_json, ) # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 1) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Houston'; # This time we include the bounding box by using the `bbox` keyword. self.assertJSONEqual( City.objects.annotate( geojson=functions.AsGeoJSON('point', bbox=True)).get( name='Victoria').geojson, victoria_json, ) # SELECT ST_AsGeoJson("geoapp_city"."point", 5, 3) FROM "geoapp_city" # WHERE "geoapp_city"."name" = 'Chicago'; # Finally, we set every available keyword. # MariaDB doesn't limit the number of decimals in bbox. if mariadb: chicago_json['bbox'] = [ -87.650175, 41.850385, -87.650175, 41.850385 ] self.assertJSONEqual( City.objects.annotate(geojson=functions.AsGeoJSON( 'point', bbox=True, crs=True, precision=5)).get( name='Chicago').geojson, chicago_json, )