-
Notifications
You must be signed in to change notification settings - Fork 31
INTPYTHON-835 Add support for GIS lookups #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NoahStapp
wants to merge
9
commits into
mongodb:main
Choose a base branch
from
NoahStapp:geospatial-spike
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c97a4fe
Initial support
NoahStapp b3a905f
Try unsupported_functions again
NoahStapp e56bf17
Fix error handling for unsupported lookups
NoahStapp 38ed40a
Linting
NoahStapp 462a54d
Avoid code churn
NoahStapp 86b1e75
Add tests for lookups
NoahStapp f4b724f
Add dwithin
NoahStapp 5ad9f60
Skip dwithin tests we don't support
NoahStapp fc3bf2c
Address Tim review
NoahStapp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,19 @@ | ||
| from django.contrib.gis.db.models.lookups import GISLookup | ||
| from django.contrib.gis.db.models.lookups import DistanceLookupFromFunction, GISLookup | ||
| from django.db import NotSupportedError | ||
|
|
||
| from django_mongodb_backend.query_utils import process_lhs, process_rhs | ||
|
|
||
| def gis_lookup(self, compiler, connection, as_expr=False): # noqa: ARG001 | ||
| raise NotSupportedError(f"MongoDB does not support the {self.lookup_name} lookup.") | ||
|
|
||
| def gis_lookup(self, compiler, connection, as_expr=False): | ||
| lhs_mql = process_lhs(self, compiler, connection, as_expr=as_expr) | ||
| rhs_mql = process_rhs(self, compiler, connection, as_expr=as_expr) | ||
| try: | ||
| rhs_op = self.get_rhs_op(connection, rhs_mql) | ||
| except KeyError as e: | ||
| raise NotSupportedError(f"MongoDB does not support the '{self.lookup_name}' lookup.") from e | ||
| return rhs_op.as_mql(lhs_mql, rhs_mql, self.rhs_params) | ||
|
|
||
|
|
||
| def register_lookups(): | ||
| GISLookup.as_mql = gis_lookup | ||
| DistanceLookupFromFunction.as_mql = gis_lookup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| """ | ||
| A collection of utility routines and classes used by the spatial | ||
| backend. | ||
| """ | ||
|
|
||
| from django.contrib.gis.db.backends.utils import SpatialOperator as _SpatialOperator | ||
|
|
||
|
|
||
| class SpatialOperator(_SpatialOperator): | ||
| """ | ||
| Class encapsulating the behavior specific to a GIS operation (used by lookups). | ||
| """ | ||
|
|
||
| def __init__(self, op=None, func=None): | ||
| self.op = op | ||
| self.func = func | ||
|
|
||
| def as_mql(self, lhs, rhs, params=None): | ||
| return self.func(lhs, rhs, self.op, params) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| [ | ||
| { | ||
| "pk": "000000000000000000000001", | ||
| "model": "gis_tests_.city", | ||
| "fields": { | ||
| "name": "Houston", | ||
| "point": "POINT (-95.363151 29.763374)" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000002", | ||
| "model": "gis_tests_.city", | ||
| "fields": { | ||
| "name": "Dallas", | ||
| "point": "POINT (-96.801611 32.782057)" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000003", | ||
| "model": "gis_tests_.city", | ||
| "fields": { | ||
| "name": "Oklahoma City", | ||
| "point": "POINT (-97.521157 34.464642)" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000004", | ||
| "model": "gis_tests_.city", | ||
| "fields": { | ||
| "name": "Wellington", | ||
| "point": "POINT (174.783117 -41.315268)" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000005", | ||
| "model": "gis_tests_.city", | ||
| "fields": { | ||
| "name": "Pueblo", | ||
| "point": "POINT (-104.609252 38.255001)" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000006", | ||
| "model": "gis_tests_.city", | ||
| "fields": { | ||
| "name": "Lawrence", | ||
| "point": "POINT (-95.235060 38.971823)" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000007", | ||
| "model": "gis_tests_.city", | ||
| "fields": { | ||
| "name": "Chicago", | ||
| "point": "POINT (-87.650175 41.850385)" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000008", | ||
| "model": "gis_tests_.city", | ||
| "fields": { | ||
| "name": "Victoria", | ||
| "point": "POINT (-123.305196 48.462611)" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000001", | ||
| "model": "gis_tests_.zipcode", | ||
| "fields" : { | ||
| "code" : "77002", | ||
| "poly" : "POLYGON ((-95.365015 29.772327, -95.362415 29.772327, -95.360915 29.771827, -95.354615 29.771827, -95.351515 29.772527, -95.350915 29.765327, -95.351015 29.762436, -95.350115 29.760328, -95.347515 29.758528, -95.352315 29.753928, -95.356415 29.756328, -95.358215 29.754028, -95.360215 29.756328, -95.363415 29.757128, -95.364014 29.75638, -95.363415 29.753928, -95.360015 29.751828, -95.361815 29.749528, -95.362715 29.750028, -95.367516 29.744128, -95.369316 29.745128, -95.373916 29.744128, -95.380116 29.738028, -95.387916 29.727929, -95.388516 29.729629, -95.387916 29.732129, -95.382916 29.737428, -95.376616 29.742228, -95.372616 29.747228, -95.378601 29.750846, -95.378616 29.752028, -95.378616 29.754428, -95.376016 29.754528, -95.374616 29.759828, -95.373616 29.761128, -95.371916 29.763928, -95.372316 29.768727, -95.365884 29.76791, -95.366015 29.767127, -95.358715 29.765327, -95.358615 29.766327, -95.359115 29.767227, -95.360215 29.767027, -95.362783 29.768267, -95.365315 29.770527, -95.365015 29.772327))" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000002", | ||
| "model": "gis_tests_.zipcode", | ||
| "fields" : { | ||
| "code" : "77005", | ||
| "poly" : "POLYGON ((-95.447918 29.727275, -95.428017 29.728729, -95.421117 29.729029, -95.418617 29.727629, -95.418517 29.726429, -95.402117 29.726629, -95.402117 29.725729, -95.395316 29.725729, -95.391916 29.726229, -95.389716 29.725829, -95.396517 29.715429, -95.397517 29.715929, -95.400917 29.711429, -95.411417 29.715029, -95.418417 29.714729, -95.418317 29.70623, -95.440818 29.70593, -95.445018 29.70683, -95.446618 29.70763, -95.447418 29.71003, -95.447918 29.727275))" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000003", | ||
| "model": "gis_tests_.zipcode", | ||
| "fields" : { | ||
| "code" : "77025", | ||
| "poly" : "POLYGON ((-95.418317 29.70623, -95.414717 29.706129, -95.414617 29.70533, -95.418217 29.70533, -95.419817 29.69533, -95.419484 29.694196, -95.417166 29.690901, -95.414517 29.69433, -95.413317 29.69263, -95.412617 29.68973, -95.412817 29.68753, -95.414087 29.685055, -95.419165 29.685428, -95.421617 29.68513, -95.425717 29.67983, -95.425017 29.67923, -95.424517 29.67763, -95.427418 29.67763, -95.438018 29.664631, -95.436713 29.664411, -95.440118 29.662231, -95.439218 29.661031, -95.437718 29.660131, -95.435718 29.659731, -95.431818 29.660331, -95.441418 29.656631, -95.441318 29.656331, -95.441818 29.656131, -95.441718 29.659031, -95.441118 29.661031, -95.446718 29.656431, -95.446518 29.673431, -95.446918 29.69013, -95.447418 29.71003, -95.446618 29.70763, -95.445018 29.70683, -95.440818 29.70593, -95.418317 29.70623))" | ||
| } | ||
| }, | ||
| { | ||
| "pk": "000000000000000000000004", | ||
| "model": "gis_tests_.zipcode", | ||
| "fields" : { | ||
| "code" : "77401", | ||
| "poly" : "POLYGON ((-95.447918 29.727275, -95.447418 29.71003, -95.446918 29.69013, -95.454318 29.68893, -95.475819 29.68903, -95.475819 29.69113, -95.484419 29.69103, -95.484519 29.69903, -95.480419 29.70133, -95.480419 29.69833, -95.474119 29.69833, -95.474119 29.70453, -95.472719 29.71283, -95.468019 29.71293, -95.468219 29.720229, -95.464018 29.720229, -95.464118 29.724529, -95.463018 29.725929, -95.459818 29.726129, -95.459918 29.720329, -95.451418 29.720429, -95.451775 29.726303, -95.451318 29.727029, -95.447918 29.727275))" | ||
| } | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,10 @@ | |
|
|
||
|
|
||
| class City(models.Model): | ||
| name = models.CharField(max_length=30) | ||
| point = models.PointField() | ||
|
Comment on lines
+5
to
6
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
|
|
||
|
|
||
| class Zipcode(models.Model): | ||
| code = models.CharField(max_length=10) | ||
| poly = models.PolygonField(geography=True) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.