Permalink
Browse files

Refined tiling code

  • Loading branch information...
1 parent 7083228 commit 4c5d4504bda540f0cdafe108e239648a5790bc1f @jrasky jrasky committed Mar 17, 2012
View
@@ -3,6 +3,8 @@ Todo list for geocamlayer
- Figure out tiling
- document protocol, interface behavior
- integration problem
+ - test suite
+ - client side
- Actually get kml working
- Top level link that will load the kml in gearth and do the right stuff
View
Binary file not shown.
View
@@ -12,7 +12,7 @@
admin.autodiscover()
from geocamLayer import models
-objects = [models.RandomFeature() for x in range(1000)]
+objects = [models.Feature.randomFeature() for x in xrange(1000)]
urlpatterns = patterns('',
# Example:
@@ -1,5 +1,6 @@
#!/usr/bin/env python
from django.core.management.base import BaseCommand, CommandError
+from django.db import transaction
from optparse import make_option
from geocamLayer.models import *
import sys
@@ -20,6 +21,7 @@ class Command(BaseCommand):
def handle(self, *args, **options):
stdout = sys.stdout
sys.stdout = self.stdout
+ transaction.commit_manually()
if not args:
print "No args found, using 1000 points"
numpoints = 1000
@@ -28,15 +30,15 @@ def handle(self, *args, **options):
except ValueError: raise CommandError("Invalid first argument")
if options['clear_db']:
print "Clearing database..."
- for feature in RandomFeature.objects.all():
- feature.delete()
+ Feature.objects.all().delete()
print "Generating %s points..." % numpoints
for i in xrange(numpoints):
sys.stdout.write('\r')
sys.stdout.write(str(int((float(i)/numpoints)*100)))
sys.stdout.write('%')
sys.stdout.flush()
- feature = RandomFeature()
+ feature = Feature.randomFeature()
feature.save()
print
+ transaction.commit()
sys.stdout = stdout
@@ -2,7 +2,7 @@
from django.core.management.base import BaseCommand, CommandError
from geocamLayer import models
from django.conf import settings
-import os, os.path, math
+import os, os.path, math, json
try: import cPickle as pickle
except ImportError: import pickle
@@ -15,14 +15,15 @@ class Command(BaseCommand):
def handle(self, *args, **options):
self.stdout.write("Getting all objects...\n")
- features = models.BaseFeature.objects.all()
+ features = models.Feature.objects.all()
self.stdout.write("%s objects in total\n" % len(features))
root = os.path.join(settings.MEDIA_ROOT, "tiles")
tiles = {}
self.stdout.write("Clustering...\n")
for point in features:
lng,lat = point.getPosition()
for zoom in xrange(10):
+ # fragment this
xf = float(lng+180)/(360./(2**(zoom+1)))
yf = float(lat+ 90)/(360./(2**(zoom+1)))
xmax = math.floor(xf)
@@ -36,18 +37,27 @@ def handle(self, *args, **options):
if not os.path.exists(tile_path):
os.makedirs(tile_path)
for y in range(int(ymin),int(ymax+1)):
- tile_file = os.path.join(tile_path, str(y))
+ tile_file = os.path.join(tile_path, str(y)+'.json')
if tile_file in tiles:
tile = tiles[tile_file]
elif os.path.exists(tile_file):
- tile = pickle.load(open(tile_file, 'rb'))
+ tile = json.load(open(tile_file))
else:
tile = {}
- if (xcell,ycell) not in tile:
- tile[(xcell,ycell)] = []
- tile[(xcell,ycell)].append(point)
+ if xcell not in tile:
+ tile[xcell] = {}
+ if ycell not in tile[xcell]:
+ tile[xcell][ycell] = []
+ point_data = {
+ 'type':'Point',
+ 'coordinates':point.getPosition(),
+ 'timestamp':str(point.getTimeStamp()),
+ 'timespan':str(point.getTimeSpan()),
+ 'name':str(point.getName()),
+ 'description':point.getDescriptionHTML()}
+ tile[xcell][ycell].append(point_data)
tiles[tile_file] = tile
self.stdout.write("Writing out...\n")
for tile_path in tiles:
- pickle.dump(tiles[tile_path],open(tile_path,'wb'))
+ json.dump(tiles[tile_path],open(tile_path, 'w'))
self.stdout.write("Done.\n")
View
@@ -12,6 +12,9 @@
class BaseFeature(models.Model):
# demo feature model demonstrating
# what you need to implement
+ class Meta:
+ abstract = True
+
def getPosition(self):
# in as tuple
return (0,0)
@@ -35,26 +38,27 @@ def getDescriptionHTML(self):
return ""
class Feature(BaseFeature):
- def __init__(self, lng, lat, timestamp=time.time(),
- timespan=datetime.timedelta(), name="Feature",
- description="Simple feature", *args, **kwargs):# properties={}, *args, **kwargs):
- BaseFeature.__init__(self, *args, **kwargs)
- self.lng, self.lat = lng,lat
- self.timestamp = timestamp
- self.timespan = timespan
- self.name = name
- self.description = description
- #self.properties = properties
+ lat = models.FloatField(null=True, blank=True)
+ lng = models.FloatField(null=True, blank=True)
+ timestamp = models.DateTimeField(null=True, blank=True)
+ timespan = models.FloatField(null=True, blank=True)
+ name = models.CharField(max_length=80)
+ description = models.TextField()
+
+ @staticmethod
+ def randomFeature():
+ feature = Feature()
+ feature.lat = random.randint(-180,180)
+ feature.lng = random.randint(-90,90)
+ feature.timestamp = datetime.datetime.now()
+ feature.timespan = random.randint(0,3)
+ feature.name = "Random Feature"
+ feature.description = "Random Feature"
+ return feature
def getPosition(self): return (self.lng,self.lat)
def getTimeStamp(self): return self.timestamp
def getTimeSpan(self): return self.timespan
def getName(self): return self.name
def getDescriptionHTML(self): return self.description
def getProperties(self): return {} # self.properties
-
-class RandomFeature(Feature):
- def __init__(self, *args, **kwargs):
- Feature.__init__(self, random.randint(-180,180), random.randint(-90,90),
- time.time(), datetime.timedelta(random.randint(0,3)),
- "Random Feature", "Random Feature", *args, **kwargs)# {}, *args, **kwargs)

0 comments on commit 4c5d450

Please sign in to comment.