I use geoDjango for an application based on OpenStreetMap in which
users will be allowed to add POIs related to ecology on a map.
I want to add RSS feeds : an user will be able to follow all the new
POIs in an area he will delimit.
I put the 4 following coordinates in the URL :
- upper_left_lat
- upper_left_lon
- lower_right_lat
- lower_right_lon
This is an example of an URL :
http://127.0.0.1:8000/myapp/rss/area/6141249,-213707,-160201,-160201/
Then, I parse the URL in the feed class and retrieve the 4
coordinates :
###############################################
class LatestPOIsByZone(Feed):
title_template = "rss_title.html"
description_template = "rss_descr.html"
def get_object(self, bits):
if len(bits) != 1:
raise ObjectDoesNotExist
coordinates = str(bits[0]).split(',')
upper_lat = float(coordinates[0])
upper_lon = float(coordinates[1])
lower_lat = float(coordinates[2])
lower_lon = float(coordinates[3])
upper = Point((upper_lat, upper_lon))
lower = Point((lower_lat, lower_lon))
area_coordinates = upper, lower
return area_coordinates
###############################################
Now, I want to return all the POI which are situated in the area
delimited by the two coordinates.
Here is the model of the POIs :
###############################################
class Marker(models.Model):
'''Marker for a POI
'''
name = models.CharField(_("Name"), max_length=150)
subcategory = models.ForeignKey(SubCategory, verbose_name=_
("Subcategory"))
avail_date = models.DateTimeField(_("Available Date"), blank=True,
null=True)
point = PointField(_("Localisation"))
...
###############################################
So I have markers in my database (PointField type) and two coordinates
designated by two POINTS structure (upper-left and lower-right) which
delimit the area. I want to retrieve from the database all the markers
situated in this area.
I think I have to make the request in the 'items(self, obj)' method
but I have no idea how to do such a thing. I have seen in the db
geodjango API requests such as :
Marker.objects.filter(point__contained=area)
Where point is my marker localisation and area a geo object
representing my area.
But I don't know if I can use it in my case and how to use it.
Thanks in advance,
CodeCRC
Based on what you are trying to do, it seems more natural to store
your area of interest (AOI) as a polygon, rather than two points.
That way you can use the spatial operators to check if points are
within your AOI.
Example:
from django.contrib.gis.geos import GEOSGeometry
AOI=GEOSGeometry('POLYGON(( 10 10, 10 20, 20 20, 20 15, 10 10))')
- Tyler
You may also be interested in the from_bbox method of geos Polygons as
well:
>>> top = 20
>>> bottom = 0
>>> left = 100
>>> right = 200
>>> from django.contrib.gis import geos
>>> p = geos.Polygon.from_bbox([left, bottom, right, top])
>>> p.wkt
'POLYGON ((100.0 0.0, 100.0 20.0, 200.0 20.0, 200.0 0.0, 100.0 0.0))'
>>> # WKT trimmed for readability
Cheers,
Cliff