django-gisserver 1.4.1__py3-none-any.whl → 2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. {django_gisserver-1.4.1.dist-info → django_gisserver-2.0.dist-info}/METADATA +23 -13
  2. django_gisserver-2.0.dist-info/RECORD +66 -0
  3. {django_gisserver-1.4.1.dist-info → django_gisserver-2.0.dist-info}/WHEEL +1 -1
  4. gisserver/__init__.py +1 -1
  5. gisserver/compat.py +23 -0
  6. gisserver/conf.py +7 -0
  7. gisserver/db.py +63 -60
  8. gisserver/exceptions.py +47 -9
  9. gisserver/extensions/__init__.py +4 -0
  10. gisserver/{parsers/fes20 → extensions}/functions.py +11 -5
  11. gisserver/extensions/queries.py +261 -0
  12. gisserver/features.py +267 -240
  13. gisserver/geometries.py +34 -39
  14. gisserver/management/__init__.py +0 -0
  15. gisserver/management/commands/__init__.py +0 -0
  16. gisserver/management/commands/loadgeojson.py +291 -0
  17. gisserver/operations/base.py +129 -305
  18. gisserver/operations/wfs20.py +428 -336
  19. gisserver/output/__init__.py +10 -48
  20. gisserver/output/base.py +198 -143
  21. gisserver/output/csv.py +81 -85
  22. gisserver/output/geojson.py +63 -72
  23. gisserver/output/gml32.py +310 -281
  24. gisserver/output/iters.py +207 -0
  25. gisserver/output/results.py +71 -30
  26. gisserver/output/stored.py +143 -0
  27. gisserver/output/utils.py +75 -154
  28. gisserver/output/xmlschema.py +86 -47
  29. gisserver/parsers/__init__.py +10 -10
  30. gisserver/parsers/ast.py +320 -0
  31. gisserver/parsers/fes20/__init__.py +15 -11
  32. gisserver/parsers/fes20/expressions.py +89 -50
  33. gisserver/parsers/fes20/filters.py +111 -43
  34. gisserver/parsers/fes20/identifiers.py +44 -26
  35. gisserver/parsers/fes20/lookups.py +144 -0
  36. gisserver/parsers/fes20/operators.py +336 -128
  37. gisserver/parsers/fes20/sorting.py +107 -34
  38. gisserver/parsers/gml/__init__.py +12 -11
  39. gisserver/parsers/gml/base.py +6 -3
  40. gisserver/parsers/gml/geometries.py +69 -35
  41. gisserver/parsers/ows/__init__.py +25 -0
  42. gisserver/parsers/ows/kvp.py +190 -0
  43. gisserver/parsers/ows/requests.py +158 -0
  44. gisserver/parsers/query.py +175 -0
  45. gisserver/parsers/values.py +26 -0
  46. gisserver/parsers/wfs20/__init__.py +37 -0
  47. gisserver/parsers/wfs20/adhoc.py +245 -0
  48. gisserver/parsers/wfs20/base.py +143 -0
  49. gisserver/parsers/wfs20/projection.py +103 -0
  50. gisserver/parsers/wfs20/requests.py +482 -0
  51. gisserver/parsers/wfs20/stored.py +192 -0
  52. gisserver/parsers/xml.py +249 -0
  53. gisserver/projection.py +357 -0
  54. gisserver/static/gisserver/index.css +12 -1
  55. gisserver/templates/gisserver/index.html +1 -1
  56. gisserver/templates/gisserver/service_description.html +2 -2
  57. gisserver/templates/gisserver/wfs/2.0.0/get_capabilities.xml +11 -11
  58. gisserver/templates/gisserver/wfs/feature_field.html +2 -2
  59. gisserver/templatetags/gisserver_tags.py +20 -0
  60. gisserver/types.py +375 -258
  61. gisserver/views.py +206 -75
  62. django_gisserver-1.4.1.dist-info/RECORD +0 -53
  63. gisserver/parsers/base.py +0 -149
  64. gisserver/parsers/fes20/query.py +0 -275
  65. gisserver/parsers/tags.py +0 -102
  66. gisserver/queries/__init__.py +0 -34
  67. gisserver/queries/adhoc.py +0 -181
  68. gisserver/queries/base.py +0 -146
  69. gisserver/queries/stored.py +0 -205
  70. gisserver/templates/gisserver/wfs/2.0.0/describe_stored_queries.xml +0 -20
  71. gisserver/templates/gisserver/wfs/2.0.0/list_stored_queries.xml +0 -14
  72. {django_gisserver-1.4.1.dist-info → django_gisserver-2.0.dist-info}/LICENSE +0 -0
  73. {django_gisserver-1.4.1.dist-info → django_gisserver-2.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,261 @@
1
+ """Storage and registry for stored queries.
2
+ These definitions follow the WFS spec.
3
+
4
+ By using the registry, custom stored queries can be registered in this server.
5
+ Out of the box, only the mandatory built-in ``GetFeatureById`` query is present.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import typing
11
+ from collections.abc import Iterable, Iterator
12
+ from dataclasses import dataclass, field
13
+ from functools import partial
14
+ from xml.etree.ElementTree import Element
15
+
16
+ from django.db.models import Q
17
+
18
+ from gisserver.exceptions import InvalidParameterValue, NotFound, OperationNotSupported
19
+ from gisserver.features import FeatureType
20
+ from gisserver.parsers import fes20
21
+ from gisserver.parsers.query import CompiledQuery
22
+ from gisserver.types import XsdTypes
23
+
24
+ if typing.TYPE_CHECKING:
25
+ from gisserver.output import SimpleFeatureCollection
26
+
27
+ __all__ = (
28
+ "GetFeatureById",
29
+ "QueryExpressionText",
30
+ "StoredQueryDescription",
31
+ "StoredQueryRegistry",
32
+ "stored_query_registry",
33
+ )
34
+
35
+ WFS_LANGUAGE = "urn:ogc:def:queryLanguage:OGC-WFS::WFS_QueryExpression" # body is <wfs:Query>
36
+ FES_LANGUAGE = "urn:ogc:def:queryLanguage:OGC-FES:Filter" # body is <fes:Filter>
37
+
38
+
39
+ @dataclass
40
+ class QueryExpressionText:
41
+ """Define the body of a stored query.
42
+
43
+ This object type is defined in the WFS spec.
44
+ It may contain a wfs:Query or wfs:StoredQuery element.
45
+ """
46
+
47
+ #: Which types the query will return.
48
+ return_feature_types: list[str] | None = None
49
+ #: The internal language of the query. Can be a WFS/FES-filter, or "python"
50
+ language: str = FES_LANGUAGE
51
+ #: Whether the implementation_text will be show or hidden from users.
52
+ is_private: bool = True
53
+
54
+ #: Body
55
+ implementation_text: str | Element | None = None
56
+
57
+
58
+ @dataclass
59
+ class StoredQueryDescription:
60
+ """WFS metadata of a stored query.
61
+ This is based on the ``<wfs:StoredQueryDescription>`` element,
62
+ and returned in ``DescribeStoredQueries``.
63
+
64
+ While it's possible to define multiple QueryExpressionText nodes
65
+ as metadata to describe a query, there is still only one implementation.
66
+ Note there is no 'typeNames=...' parameter for stored queries.
67
+ Only direct parameters act as input.
68
+ """
69
+
70
+ #: The ID of the query
71
+ id: str
72
+ #: User-visible title
73
+ title: str
74
+ #: User-visible description
75
+ abstract: str
76
+ #: Parameter declaration
77
+ parameters: dict[str, XsdTypes]
78
+
79
+ #: Metadata describing the query body
80
+ expressions: list[QueryExpressionText] = field(
81
+ default_factory=lambda: [QueryExpressionText(language=FES_LANGUAGE)]
82
+ )
83
+
84
+ #: Python-based implementation for the query.
85
+ implementation_class: type[StoredQueryImplementation] = field(init=False, default=None)
86
+
87
+
88
+ class StoredQueryImplementation:
89
+ """A custom stored query.
90
+
91
+ This receives the parameters as init arguments,
92
+ and should implement :meth:`build_query`.
93
+ The function is registered using ``StoredQueryRegistry.register()``.
94
+ """
95
+
96
+ # Registered metadata
97
+ _meta: StoredQueryDescription
98
+
99
+ # Allow queries to return only the XML nodes, without any <wfs:FeatureCollection> wrapper.
100
+ has_standalone_output: bool = False
101
+
102
+ def __repr__(self):
103
+ return f"<{self.__class__.__name__} implementing '{self._meta.id}'>"
104
+
105
+ def bind(self, source_query, feature_types: list[FeatureType]):
106
+ """Associate this query with the application data."""
107
+ self.source_query = source_query
108
+ self.feature_types = feature_types
109
+ if len(feature_types) > 1:
110
+ raise OperationNotSupported("Join queries are not supported", locator="typeNames")
111
+
112
+ def get_type_names(self) -> list[str]:
113
+ """Tell which type names this query applies to."""
114
+ raise NotImplementedError()
115
+
116
+ def build_query(self, compiler: CompiledQuery) -> Q | None:
117
+ """Contribute our filter expression to the internal query.
118
+
119
+ This should add the filter expressions to the internal query compiler.
120
+ The top-level ``<wfs:StoredQuery>`` object will add
121
+ the ``<wfs:PropertyName>`` logic and other elements.
122
+ """
123
+ raise NotImplementedError()
124
+
125
+ def finalize_results(self, result: SimpleFeatureCollection):
126
+ """Hook to allow subclasses to inspect the results."""
127
+
128
+
129
+ class StoredQueryRegistry:
130
+ """Registry of functions to be callable by <wfs:StoredQuery>."""
131
+
132
+ def __init__(self):
133
+ self.stored_queries: dict[str, type(StoredQueryImplementation)] = {}
134
+
135
+ def __bool__(self):
136
+ return bool(self.stored_queries)
137
+
138
+ def __iter__(self) -> Iterator[StoredQueryDescription]:
139
+ return iter(self.stored_queries.values())
140
+
141
+ def get_queries(self) -> Iterable[StoredQueryDescription]:
142
+ """Find all descriptions for stored queries."""
143
+ return self.stored_queries.values()
144
+
145
+ def register(
146
+ self,
147
+ meta: StoredQueryDescription | None = None,
148
+ query_expression: type[StoredQueryImplementation] | None = None,
149
+ **meta_kwargs,
150
+ ):
151
+ """Register a custom class that handles a stored query.
152
+ This function can be used as decorator or normal call.
153
+ """
154
+ if meta is None:
155
+ meta = StoredQueryDescription(**meta_kwargs)
156
+ elif meta_kwargs:
157
+ raise TypeError("Either provide the 'meta' object or 'meta_kwargs'")
158
+
159
+ if query_expression is not None:
160
+ self._register(meta, query_expression)
161
+ return meta
162
+ else:
163
+ return partial(self._register, meta) # decorator effect.
164
+
165
+ def _register(
166
+ self, meta: StoredQueryDescription, implementation_class: type[StoredQueryImplementation]
167
+ ):
168
+ """Internal registration method."""
169
+ if not issubclass(implementation_class, StoredQueryImplementation):
170
+ raise TypeError(f"Expecting {StoredQueryImplementation}' subclass")
171
+ if meta.implementation_class is not None:
172
+ raise RuntimeError("Can't register same StoredQueryDescription again.")
173
+
174
+ # for now link both. There is always a single implementation for the metadata.
175
+ meta.implementation_class = implementation_class
176
+ self.stored_queries[meta.id] = meta
177
+
178
+ def resolve_query(self, query_id) -> type[StoredQueryDescription]:
179
+ """Find the stored procedure using the ID."""
180
+ try:
181
+ return self.stored_queries[query_id]
182
+ except KeyError:
183
+ raise InvalidParameterValue(
184
+ f"Stored query does not exist: {query_id}",
185
+ locator="STOREDQUERY_ID",
186
+ ) from None
187
+
188
+
189
+ stored_query_registry = StoredQueryRegistry()
190
+
191
+
192
+ @stored_query_registry.register(
193
+ id="urn:ogc:def:query:OGC-WFS::GetFeatureById",
194
+ title="Get feature by identifier",
195
+ abstract="Returns the single feature that corresponds with the ID argument",
196
+ parameters={"id": XsdTypes.string},
197
+ expressions=[QueryExpressionText(language=WFS_LANGUAGE)],
198
+ )
199
+ class GetFeatureById(StoredQueryImplementation):
200
+ """The stored query for GetFeatureById.
201
+
202
+ This can be called using::
203
+
204
+ <wfs:StoredQuery id="urn:ogc:def:query:OGC-WFS::GetFeatureById">
205
+ <wfs:Parameter name="ID">typename.ID</wfs:Parameter>
206
+ </wfs:StoredQuery>
207
+
208
+ or using KVP syntax::
209
+
210
+ ?...&REQUEST=GetFeature&STOREDQUERY_ID=urn:ogc:def:query:OGC-WFS::GetFeatureById&ID=typename.ID
211
+
212
+ The execution of the ``GetFeatureById`` query is essentially the same as::
213
+
214
+ <wfs:Query xmlns:wfs='..." xmlns:fes='...'>
215
+ <fes:Filter><fes:ResourceId rid='{ID}'/></fes:Filter>
216
+ </wfs:Query>
217
+
218
+ or::
219
+
220
+ <wfs:Query typeName="{typename}">
221
+ <fes:Filter>
222
+ <fes:PropertyIsEqualTo>
223
+ <fes:ValueReference>{primary-key-field}</fes:ValueReference>
224
+ <fes:Literal>{ID-value}</fes:Literal>
225
+ </fes:PropertyIsEqualTo>
226
+ </fes:Filter>
227
+ </wfs:Query>
228
+
229
+ Except that the response is supposed to contain only the item itself.
230
+ """
231
+
232
+ # Projection of GetFeatureById only returns the document nodes, not a <wfs:FeatureCollection> wrapper
233
+ has_standalone_output = True
234
+
235
+ def __init__(self, id: str, ns_aliases: dict[str, str]):
236
+ """Initialize the query with the request parameters."""
237
+ if "." not in id:
238
+ # Always report this as 404
239
+ raise NotFound("Expected typeName.id for ID parameter", locator="ID") from None
240
+
241
+ # GetFeatureById is essentially a ResourceId lookup, reuse that logic here.
242
+ self.resource_id = fes20.ResourceId.from_string(id, ns_aliases)
243
+
244
+ def get_type_names(self) -> list[str]:
245
+ """Tell which type names this query applies to."""
246
+ return [self.resource_id.get_type_name()]
247
+
248
+ def build_query(self, compiler: CompiledQuery) -> Q:
249
+ """Contribute our filter expression to the internal query."""
250
+ try:
251
+ return self.resource_id.build_query(compiler)
252
+ except InvalidParameterValue as e:
253
+ raise InvalidParameterValue(f"Invalid ID value: {e.__cause__}", locator="ID") from e
254
+
255
+ def finalize_results(self, results: SimpleFeatureCollection):
256
+ """Override to implement 404 checking."""
257
+ # Directly attempt to collect the data.
258
+ # Avoid having to do that in the output renderer.
259
+ if results.first() is None:
260
+ # WFS 2.0.2: Return NotFound instead of InvalidParameterValue
261
+ raise NotFound(f"Feature not found with ID {self.resource_id.rid}.", locator="ID")