(self)
| 43 | fixtures = ["initial"] |
| 44 | |
| 45 | def test_asgeojson(self): |
| 46 | if not connection.features.has_AsGeoJSON_function: |
| 47 | with self.assertRaises(NotSupportedError): |
| 48 | list(Country.objects.annotate(json=functions.AsGeoJSON("mpoly"))) |
| 49 | return |
| 50 | |
| 51 | pueblo_json = '{"type":"Point","coordinates":[-104.609252,38.255001]}' |
| 52 | houston_json = json.loads( |
| 53 | '{"type":"Point","crs":{"type":"name","properties":' |
| 54 | '{"name":"EPSG:4326"}},"coordinates":[-95.363151,29.763374]}' |
| 55 | ) |
| 56 | victoria_json = json.loads( |
| 57 | '{"type":"Point",' |
| 58 | '"bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],' |
| 59 | '"coordinates":[-123.305196,48.462611]}' |
| 60 | ) |
| 61 | chicago_json = json.loads( |
| 62 | '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},' |
| 63 | '"bbox":[-87.65018,41.85039,-87.65018,41.85039],' |
| 64 | '"coordinates":[-87.65018,41.85039]}' |
| 65 | ) |
| 66 | if "crs" in connection.features.unsupported_geojson_options: |
| 67 | del houston_json["crs"] |
| 68 | del chicago_json["crs"] |
| 69 | if "bbox" in connection.features.unsupported_geojson_options: |
| 70 | del chicago_json["bbox"] |
| 71 | del victoria_json["bbox"] |
| 72 | if "precision" in connection.features.unsupported_geojson_options: |
| 73 | chicago_json["coordinates"] = [-87.650175, 41.850385] |
| 74 | |
| 75 | # Precision argument should only be an integer |
| 76 | with self.assertRaises(TypeError): |
| 77 | City.objects.annotate(geojson=functions.AsGeoJSON("point", precision="foo")) |
| 78 | |
| 79 | # Reference queries and values. |
| 80 | # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 0) |
| 81 | # FROM "geoapp_city" WHERE "geoapp_city"."name" = 'Pueblo'; |
| 82 | self.assertJSONEqual( |
| 83 | pueblo_json, |
| 84 | City.objects.annotate(geojson=functions.AsGeoJSON("point")) |
| 85 | .get(name="Pueblo") |
| 86 | .geojson, |
| 87 | ) |
| 88 | |
| 89 | # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 2) FROM "geoapp_city" |
| 90 | # WHERE "geoapp_city"."name" = 'Houston'; |
| 91 | # This time we want to include the CRS by using the `crs` keyword. |
| 92 | self.assertJSONEqual( |
| 93 | City.objects.annotate(json=functions.AsGeoJSON("point", crs=True)) |
| 94 | .get(name="Houston") |
| 95 | .json, |
| 96 | houston_json, |
| 97 | ) |
| 98 | |
| 99 | # SELECT ST_AsGeoJson("geoapp_city"."point", 8, 1) FROM "geoapp_city" |
| 100 | # WHERE "geoapp_city"."name" = 'Houston'; |
| 101 | # This time we include the bounding box by using the `bbox` keyword. |
| 102 | self.assertJSONEqual( |
nothing calls this directly
no test coverage detected