Region and Subregion model have no Geometry (area|location) - no way to query distance
kiddhustle opened this issue · 3 comments
Checklist
- I have verified that I am using a GIS-enabled database, such as PostGIS or Spatialite.
- I have verified that that issue exists against the
master
branch of django-cities. - I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
- I have reduced the issue to the simplest possible case.
- I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)
from django.contrib.gis.db.models.functions import Distance
start_place = City.objects.filter(country__name='United Kingdom').get(name='London')
result = Region.objects.all().annotate(dist=Distance('location', start_place.location)).exclude(id=start_place.id).filter(name='Scotland').order_by('-dist')
or
```result = Region.objects.all().annotate(dist=Distance('location', start_place.location)).exclude(id=start_place.id).filter(name='Scotland').order_by('-dist')`
Expected behavior
To return the distance from Scotland region
Actual behavior
Following errors:
FieldError: Cannot resolve keyword 'location' into field. Choices are: alt_names, cities, code, country, country_id, id, name, name_std, postal_codes, slug, subregions
FieldError: Cannot resolve keyword 'area' into field. Choices are: alt_names, cities, code, country, country_id, id, name, name_std, postal_codes, slug, subregions
Hi, thank you for reporting this, but this problem is actually much more complex than it might seem at a glance.
If you would like to have a point that represents a region or a subregion: where exactly do you want that point to be? The centroid of the region/subregion? What if that point is someplace far away from any city or sign of civilization? The capital of the region/subregion? Some places have multiple capitals - which one do I pick?
If you're trying to find the distance between two regions/subregions: which points do you want to use? The distance between the centroids? That wouldn't ever really be accurate. The distance between the closest points on each region/subregion? That also wouldn't ever be accurate. The distance between the farthest points on each region/subregion? Also wouldn't ever be accurate.
Really, representing an entire area as a point is the fundamental problem. I probably won't ever implement that for something as large as a region/subregion. There's simply no reasonable and generic way to represent an area as a point.
So lets talk about areas (or "boundaries" in GIS terminology). I would absolutely love to add boundaries to all of the models in the app! However, there isn't really a single good source for that data. Specifically I would like a single source that is:
- international - only the US has very good maps (put out by the USGS - thank you!), the rest of the world has no good maps
- accurate - I would like accurate sources
- legally friendly - the terms and conditions for using some maps preclude them from use in this project - some sources may require payment, even to download or use a sample, or they may require the use of closed source or expensive software to use
- consistent - one single file format
Furthermore, there is already a PR that attempts to implement this: #159.
Closing, because this is a duplicate of that. Please dig through the previously reported issues and pull requests before opening a new one.