Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moving generic geodjango models and views into sample app, leaving gh…

…eat more simple.
  • Loading branch information...
commit 048840e7244b9a22effca9a32670e0532581afed 1 parent 269ffc0
Timothy Caro-Bruce authored
39 gheat/managers.py
View
@@ -1,39 +0,0 @@
-# -*- coding: utf-8 -*-
-from django.db import models
-
-class PointManager(models.Manager):
-
- def actives(self):
- return self.all()
-
- def points_inside(self,tile):
- '''
- Search all the points inside the Tile
- '''
- lat1, lat2, lng1, lng2 = tile.llbound
- qs = self.filter(
- latitude__lte=lat1,
- latitude__gte=lat2,
- longitude__lte=lng1,
- longitude__gte=lng2,
- density__gt=0,
- )
- return qs
-
- def num_points(self,tile,modtime=None):
- '''
- Count the number of points in a tile for a certain time
- '''
- qs = self.points_inside(tile)
- if modtime:
- qs.filter(modtime__gt=modtime)
-
- return qs.count()
-
-
- def clear_points(self):
- '''
- Clear all the points of the database
- '''
- self.actives().delete()
-
18 gheat/models.py
View
@@ -1,18 +0,0 @@
-from django.contrib.gis.db import models
-
-# Create your models here.
-class Point(models.Model):
- """
- A simple representation of a point inside the gheat database
- """
- uid = models.CharField(max_length=100, name='unique identifier')
- geometry = models.PointField()
- modtime = models.DateTimeField(auto_now = True,
- name='Last modification time', null=True)
- density = models.PositiveIntegerField(default=0, editable=False,
- name='density of the current point')
-
- objects = models.GeoManager()
-
- class Meta:
- unique_together = ('uid',)
11 gheat/urls.py
View
@@ -1,11 +0,0 @@
-# -*- coding: utf-8 -*-
-from django.conf.urls.defaults import *
-
-urlpatterns = patterns('gheat.views',
- url(
- # Example : today/fire/12/3,2.png
- regex = r'^(?P<color_scheme>\w+)/(?P<zoom>\d+)/(?P<x>\d+),(?P<y>\d+).png$',
- view = 'serve_tile',
- name = 'serve_tile',
- ),
- )
70 gheat/views.py
View
@@ -1,70 +0,0 @@
-import os.path
-from django.http import HttpResponseRedirect
-from gheat import dots
-from gheat import backend, color_schemes, translate, ROOT, log, \
- ALWAYS_BUILD
-from gheat.models import Point
-
-from django.http import HttpResponseBadRequest
-from django.conf import settings
-from django.views.static import serve
-
-# Create your views here.
-def serve_tile(request,color_scheme,zoom,x,y):
- '''
- Responsible for serving png files of the tile for the heat map
-
- This view will try to serve the file from the filesystem in case already
- exists otherwise just try to genereate it, and serve it.
- '''
-
- # Asserting request is a correct one
- try:
- assert color_scheme in color_schemes, ( "bad color_scheme: "
- + color_scheme
- )
- assert zoom.isdigit() and x.isdigit() and y.isdigit(), "not digits"
- zoom = int(zoom)
- x = int(x)
- y = int(y)
- assert 0 <= zoom <= 30, "bad zoom: %d" % zoom
- except AssertionError, err:
- return HttpResponseBadRequest()
-
- # @TODO: We should return the file in case is already present
- # Also we have to implement a redirection to the front end in case we are not in debug mode ... should we ?
-
- fspath = generate_tile(request,color_scheme,zoom,x,y)
-
- if settings.DEBUG:
- return serve(request, fspath, '/')
- else:
- return HttpResponseRedirect(fspath.replace(ROOT, '/site_media/gheat/'))
-
-
-def generate_tile(request,color_scheme,zoom,x,y):
- '''
- This view will generate the png file for the current request
- '''
- path = request.path
-
- path = path[path.index(color_scheme)-1:] # Removing the /gheat/ from the url
-
- fspath = translate(ROOT, path)
-
- if os.path.exists(fspath):
- return fspath
-
- color_scheme = color_schemes[color_scheme]
- tile = backend.Tile(Point.objects.all(), color_scheme, dots, zoom, x, y, fspath, last_modified_field='modtime')
- if tile.is_empty():
- fspath = color_scheme.get_empty_fspath(zoom)
- log.debug('serving empty tile, request: %s, file %s' % (path,fspath))
- elif tile.is_stale() or ALWAYS_BUILD:
- log.debug('rebuilding %s' % path)
- tile.rebuild()
- tile.save()
- else:
- log.debug('serving cached tile %s' % path)
-
- return fspath
BIN  samples/persisted/db/persisted.db
View
Binary file not shown
0  samples/persisted/home/jobs/__init__.py
View
No changes.
0  samples/persisted/home/jobs/daily/__init__.py
View
No changes.
0  samples/persisted/home/jobs/hourly/__init__.py
View
No changes.
42 samples/persisted/home/jobs/hourly/update_gheat_points.py
View
@@ -1,42 +0,0 @@
-import os.path
-import os, logging
-import shutil
-from datetime import datetime, timedelta
-
-from django_extensions.management.jobs import HourlyJob
-
-from gheat.models import Point
-from gheat import ROOT
-import feedparser
-
-
-feeds = [
- #'http://earthquake.usgs.gov/eqcenter/catalogs/eqs7day-M5.xml',
- 'http://www.earthpublisher.com/georss.php',
- ]
-
-class Job(HourlyJob):
- help = 'Calculate the points for the gHeat server, fetching them from an'\
- ' internet public georss service'
-
-
- def execute(self):
- # We should iterate over all the possible filters, any place to have this possible filters ??? __init__.py ? settings.py ?
- shutil.rmtree(ROOT)
- Point.objects.clear_points() # Should be rethought ... probably is no needed ... or it is ?
-
- total_points = 0
-
- for feed in feeds:
- f = feedparser.parse(feed)
-
- for entry in f.entries:
- Point(uid=entry.id if getattr(entry, 'id', None) else total_points,
- latitude=entry.geo_lat,
- longitude=entry.geo_long,
- density=1,
- ).save()
- total_points += 1
-
- logging.info('Finnished calculating new points, total points: %s'
- % total_points)
0  samples/persisted/home/jobs/monthly/__init__.py
View
No changes.
0  samples/persisted/home/jobs/weekly/__init__.py
View
No changes.
17 samples/persisted/home/models.py
View
@@ -1,3 +1,18 @@
-from django.db import models
+from django.contrib.gis.db import models
# Create your models here.
+class Point(models.Model):
+ """
+ A simple representation of a point inside the gheat database
+ """
+ uid = models.CharField(max_length=100, name='unique identifier')
+ geometry = models.PointField()
+ modtime = models.DateTimeField(auto_now = True,
+ name='Last modification time', null=True)
+ density = models.PositiveIntegerField(default=0, editable=False,
+ name='density of the current point')
+
+ objects = models.GeoManager()
+
+ class Meta:
+ unique_together = ('uid',)
17 samples/persisted/home/urls.py
View
@@ -2,16 +2,15 @@
from django.views.generic.simple import direct_to_template
from django.conf import settings
-urlpatterns = patterns('',
- ('^about/$', direct_to_template, {
- 'template': 'about.html'
- })
-)
-
-
urlpatterns = patterns('home.views',
url(
- regex = r'',
+ # Example : today/fire/12/3,2.png
+ regex = r'^gheat/(?P<color_scheme>\w+)/(?P<zoom>\d+)/(?P<x>\d+),(?P<y>\d+).png$',
+ view = 'serve_tile',
+ name = 'serve_tile',
+ ),
+ url(
+ regex = r'^home',
view = direct_to_template,
name = 'home',
kwargs = {
@@ -20,5 +19,5 @@
'google_key':settings.GOOGLE_MAPS_KEY,
}
}
- ),
+ ),
)
69 samples/persisted/home/views.py
View
@@ -1,3 +1,70 @@
+import os.path
+from django.http import HttpResponseRedirect
+from gheat import dots, backend, color_schemes, translate, ROOT, log, \
+ ALWAYS_BUILD
+from home.models import Point
-
+from django.http import HttpResponseBadRequest
+from django.conf import settings
+from django.views.static import serve
+
+# Create your views here.
+def serve_tile(request,color_scheme,zoom,x,y):
+ '''
+ Responsible for serving png files of the tile for the heat map
+
+ This view will try to serve the file from the filesystem in case already
+ exists otherwise just try to genereate it, and serve it.
+ '''
+
+ # Asserting request is a correct one
+ try:
+ assert color_scheme in color_schemes, ( "bad color_scheme: "
+ + color_scheme
+ )
+ assert zoom.isdigit() and x.isdigit() and y.isdigit(), "not digits"
+ zoom = int(zoom)
+ x = int(x)
+ y = int(y)
+ assert 0 <= zoom <= 30, "bad zoom: %d" % zoom
+ except AssertionError, err:
+ return HttpResponseBadRequest()
+
+ # @TODO: We should return the file in case is already present
+ # Also we have to implement a redirection to the front end in case we are not in debug mode ... should we ?
+
+ fspath = generate_tile(request,color_scheme,zoom,x,y)
+
+ if settings.DEBUG:
+ return serve(request, fspath, '/')
+ else:
+ return HttpResponseRedirect(fspath.replace(ROOT, '/site_media/gheat/'))
+
+
+def generate_tile(request,color_scheme,zoom,x,y):
+ '''
+ This view will generate the png file for the current request
+ '''
+ path = request.path
+
+ path = path[path.index(color_scheme)-1:] # Removing the /gheat/ from the url
+
+ fspath = translate(ROOT, path)
+
+ if os.path.exists(fspath):
+ return fspath
+
+ color_scheme = color_schemes[color_scheme]
+ tile = backend.Tile(Point.objects.all(), color_scheme, dots, zoom, x, y, fspath, last_modified_field='modtime')
+ if tile.is_empty():
+ fspath = color_scheme.get_empty_fspath(zoom)
+ log.debug('serving empty tile, request: %s, file %s' % (path,fspath))
+ elif tile.is_stale() or ALWAYS_BUILD:
+ log.debug('rebuilding %s' % path)
+ tile.rebuild()
+ tile.save()
+ else:
+ log.debug('serving cached tile %s' % path)
+
+ return fspath
10 samples/persisted/urls.py
View
@@ -1,12 +1,4 @@
from django.conf.urls.defaults import *
-# Uncomment the next two lines to enable the admin:
-# from django.contrib import admin
-# admin.autodiscover()
+from home.urls import urlpatterns
-urlpatterns = patterns('',
- # Example:
- (r'^home/', include('home.urls')),
- (r'^gheat/', include('gheat.urls')),
-
-)
Please sign in to comment.
Something went wrong with that request. Please try again.