-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
297 lines (262 loc) · 10.4 KB
/
models.py
File metadata and controls
297 lines (262 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import zipfile
from urllib import urlencode
from urllib2 import urlopen
import json
from django import forms
from django.contrib import gis
from django.contrib.auth.models import User
from django.core import validators
from django.contrib.gis.db import models
# requires GeoDjango Libraries
from django.contrib.gis.gdal import DataSource
# the basepath for file uploads (needed to read shapefiles)
from settings import MEDIA_ROOT
def get_upload_path(instance, filename):
return instance.get_upload_path(filename)
class Authored(models.Model):
"""For things made by people """
author = models.ForeignKey(User)
class Meta:
abstract=True
class Named(models.Model):
"""just putting names on models"""
name = models.CharField(max_length=200, null=True, blank=True)
class Meta:
abstract=True
class Dated(models.Model):
date_added = models.DateTimeField(auto_now_add=True)
date_edited = models.DateTimeField(auto_now=True)
class Meta:
abstract=True
class Noted(models.Model):
notes = models.TextField(null=True, blank=True)
class Meta:
abstract=True
class GeomType(models.Model):
"""adding geomtery type"""
geometry_type = models.CharField(max_length=200, null=True, blank=True)
class Meta:
abstract=True
class Bboxes(models.Model):
"""shapefile's bounding box"""
bbox = models.TextField()
class Meta:
abstract=True
class GeomFields(models.Model):
"""adding attribute fields"""
fields = models.TextField()
class Meta:
abstract=True
class FilePath(models.Model):
"""adding attribute fields"""
pathy = models.TextField()
class Meta:
abstract=True
class OGRGeom(models.Model):
"""adding attribute fields"""
ogr_geom = models.GeometryField()
objects = models.GeoManager()
class Meta:
abstract=True
class Lookup(Named):
"""name and slug"""
slug = models.SlugField(max_length=100, null=True, blank=True)
class Meta:
abstract=True
class DataFile(Dated):
"""Data files represent individual file uploads.
They are used to construct DataLayers.
"""
file = models.FileField(upload_to=get_upload_path)
upload = models.ForeignKey('UploadEvent', null=True, blank=True)
def get_upload_path(self, filename):
return 'uploads/%s/%s' % (self.upload.user.username, filename)
def abs_path(self):
"""returns the full path of the zip file"""
return os.path.join( MEDIA_ROOT, self.file.__unicode__())
def extract_path(self):
"""returns a directory path for extracting zip files to"""
return os.path.splitext( self.abs_path() )[0]
def path_of_part(self, ext):
"""give an file extension of a specific file within the zip file, and
get an absolute path to the extracted file with that extension.
Assumes that the contents have been extracted.
Returns `None` if the file can't be found
"""
pieces = os.listdir( self.extract_path() )
piece = [p for p in pieces if ext in p]
if not piece:
return None
else:
return os.path.join( self.extract_path(), piece[0] )
def __unicode__(self):
return "DataFile: %s" % self.file.url
def get_layer_data(self):
"""extracts relevant data for building LayerData objects
meant to be used as initial data for LayerReview Forms
"""
data = {}
data['data_file_id'] = self.id
abs_path = self.abs_path()
# see if we need to extract it
extract_dir = self.extract_path()
basename = os.path.split( extract_dir )[1]
if not os.path.isdir( extract_dir ):
# extract it to a directory with that name
os.mkdir( extract_dir )
zip_file = zipfile.ZipFile( self.file )
zip_file.extractall( extract_dir )
# get shape type
shape_path = self.path_of_part('.shp')
ds = DataSource( shape_path )
layer = ds[0]
data['geometry_type'] = layer.geom_type.name
data['name'] = layer.name
data['fields'] = layer.fields
data['bbox'] = layer.extent.tuple
data['tags'] = ''
data['pathy'] = shape_path
if layer.srs:
srs = layer.srs
try:
srs.identify_epsg()
data['srs'] = srs['AUTHORITY'] +':'+srs['AUTHORITY', 1]
except:
data['srs'] = None
if not data['srs']:
# get .prj text
prj_path = self.path_of_part('.prj')
if prj_path:
prj_text = open(prj_path, 'r').read()
data['notes'] = prj_text
data['srs'] = 'No known Spatial Reference System'
return data
def get_srs(self):
"""takes the prj data and sends it to the prj2epsg API.
The API returns the srs code if found.
"""
api_srs = {}
prj_path = self.path_of_part('.prj')
if prj_path:
prj_text = open(prj_path, 'r').read()
query = urlencode({
'exact' : False,
'error' : True,
'terms' : prj_text})
webres = urlopen('http://prj2epsg.org/search.json', query)
jres = json.loads(webres.read())
if jres['codes']:
api_srs['message'] = 'An exact match was found'
api_srs['srs'] = int(jres['codes'][0]['code'])
else:
api_srs['message'] = 'No exact match was found'
ason_srs['srs'] = 'No known Spatial Reference System'
return api_srs
def get_centroids(self, spatial_ref):
'''
Gets the centroids of the site layer to do a distance query based on them.
Converts different type of geometries int point objects.
'''
shp_path = self.path_of_part('.shp')
site_ds = DataSource(shp_path)
site_layer = site_ds[0]
geoms = [ ]
for feature in site_layer:
#Geometries can only be transformed if they have a .prj file
if feature.geom.srs:
polygon = feature.geom.transform(spatial_ref,True)
#Get the centroids to calculate distances.
if polygon.geom_type == 'POINT':
centroids = polygon
geoms.append(centroids)
elif polygon.geom_type == 'POLYGON':
centroids = polygon.centroid
geoms.append(centroids)
#Linestrings and geometry collections can't return centroids,
#so we get the bbox and then the centroid.
elif polygon.geom_type == 'LINESTRING' or 'MULTIPOINT' or 'MULTILINESTRING' or 'MULTIPOLYGON':
bbox = polygon.envelope.wkt
centroids = OGRGeometry(bbox).centroid
geoms.append(centroids)
return geoms
class DataLayer(Named, Authored, Dated, Noted, GeomType,FilePath):
srs = models.CharField(max_length=50, null=True, blank=True)
files = models.ManyToManyField('DataFile', null=True, blank=True )
tags = models.CharField(max_length=50, null=True, blank=True)
objects = models.GeoManager()
def get_browsing_data(self):
obj = vars(self)
tags = self.tag_set.all()
if tags:
obj['tags'] = ' '.join( [t.name for t in tags] )
else:
obj['tags'] = ''
return obj
def __unicode__(self):
return "DataLayer: %s" % self.name
class UploadEvent(models.Model):
user = models.ForeignKey(User)
date = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return "UploadEvent: %s" % self.date
class Tag(Lookup, Dated, Noted):
layers = models.ManyToManyField(DataLayer)
def __unicode__(self):
return "Tag: %s" % self.slug
class Attribute(Named):
layer = models.ForeignKey(DataLayer)
data_type = models.CharField(max_length=100)
def __unicode__(self):
return "Attribute: %s" % self.name
class SiteConfiguration(Named, Authored, Dated, Noted):
"""A model for storing the different site configurations that someone has
made. It must have a site_layer that defines the separate sites.
It can add other layers (these should maybe be ordered with
django-sortedm2m )
It has a radius and srs code.
the srs attribute is defined so that it could be proj or WKT text or an
EPSG code. It will be used to define the coordinate system for the
built sites.
This should maybe be immutable. If something is changed, it should make
a new instance, so that we always can track down the settings used for
a particular SiteSet.
"""
site_layer = models.ForeignKey('DataLayer',
related_name='siteconfiguration_site')
other_layers = models.ManyToManyField('DataLayer',
related_name='siteconfiguration_other',
null=True, blank=True)
radius = models.IntegerField( default=1000 )
srs = models.CharField( max_length=500, null=True, blank=True)
objects = models.GeoManager()
def __unicode__(self):
return "SiteConfiguration: %s" % self.name
class SiteSet(Dated, Authored, Named): #I need to add the name of the site configuration!
"""A model for managing a set of generated sites.
Someone can generate sites more than once from the same
SiteConfiguration.
A SiteSet has a set of Sites
"""
configuration = models.ForeignKey('SiteConfiguration') #just quickly getting something ready for venice.
geoJson = models.TextField() # if it breaks, I need to get rid of this frome the DB
def __unicode__(self):
return "SiteConfiguration: %s" % self.name
class Site(models.Model):
"""A model for managing an individual generated Site.
A Site belongs to a Site Set and has a set of LocalLayers
"""
site_id = models.IntegerField()
site_set = models.ForeignKey('SiteSet')
class LocalLayer(models.Model):
"""A model for managing an individual generated layer in an individual
Site.
A LocalLayer belongs to a Site.
"""
site = models.ForeignKey('Site')
origin_layer = models.ForeignKey('DataLayer')
def create_from_shapefile(self, path):
ds = DataSource(path)
layer = ds[0]
for feature in layer:
DataLayer.objects.create(geometry=feature['geometry'], field=feature['field'])