48
48
49
49
import click
50
50
import csv
51
- # from geoalchemy2.shape import to_shape
52
- from geoalchemy2 .types import Geometry
53
51
import json
54
- from sqlalchemy import (Boolean , Column , create_engine , Date , DateTime ,
52
+ from sqlalchemy import (Boolean , Column , create_engine , Date , DateTime , Float ,
55
53
Enum , ForeignKey , Integer , String , Time , UnicodeText ,
56
54
UniqueConstraint )
57
55
from sqlalchemy .exc import OperationalError , ProgrammingError
58
56
from sqlalchemy .ext .declarative import declarative_base
59
57
from sqlalchemy .orm import relationship
60
58
61
- from woudc_data_registry import registry , util
59
+ from woudc_data_registry import registry
60
+ from woudc_data_registry .util import point2geojsongeometry
62
61
63
62
base = declarative_base ()
64
63
@@ -139,7 +138,8 @@ class Contributor(base):
139
138
140
139
last_validated_datetime = Column (DateTime , nullable = False ,
141
140
default = datetime .utcnow ())
142
- location = Column (Geometry ('POINT' , srid = 4326 ), nullable = False )
141
+ x = Column (Float , nullable = False )
142
+ y = Column (Float , nullable = False )
143
143
144
144
# relationships
145
145
country = relationship ('Country' , backref = __tablename__ )
@@ -154,14 +154,15 @@ def __init__(self, dict_):
154
154
self .url = dict_ ['url' ]
155
155
self .email = dict_ ['email' ]
156
156
self .ftp_username = dict_ ['ftp_username' ]
157
- self .location = util .point2ewkt (dict_ ['x' ], dict_ ['y' ])
157
+ self .x = dict_ ['x' ]
158
+ self .y = dict_ ['y' ]
158
159
159
160
@property
160
161
def __geo_interface__ (self ):
161
162
return {
162
163
'id' : self .identifier ,
163
164
'type' : 'Feature' ,
164
- # 'geometry': to_shape (self.location).__geo_interface__ ,
165
+ 'geometry' : point2geojsongeometry (self .x , self . y ) ,
165
166
'properties' : {
166
167
'name' : self .name ,
167
168
'country_id' : self .country_id ,
@@ -222,8 +223,9 @@ class Station(base):
222
223
last_validated_datetime = Column (DateTime , nullable = False ,
223
224
default = datetime .utcnow ())
224
225
225
- # location = Column(Geometry('POINT', srid=4326), nullable=False)
226
- location = Column (Geometry (srid = 0 ), nullable = False )
226
+ x = Column (Float , nullable = False )
227
+ y = Column (Float , nullable = False )
228
+ z = Column (Float , nullable = True )
227
229
228
230
# relationships
229
231
country = relationship ('Country' , backref = __tablename__ )
@@ -240,8 +242,32 @@ def __init__(self, dict_):
240
242
self .contributor_id = dict_ ['contributor_id' ]
241
243
self .wmo_region = dict_ ['wmo_region' ]
242
244
self .active_start_date = dict_ ['active_start_date' ]
243
- self .active_end_datet = dict_ ['active_end_date' ]
244
- self .location = util .point2ewkt (dict_ ['x' ], dict_ ['y' ], dict_ ['z' ])
245
+ self .active_end_date = dict_ ['active_end_date' ]
246
+ self .x = dict_ ['x' ]
247
+ self .y = dict_ ['y' ]
248
+ self .z = dict_ ['z' ]
249
+
250
+ def __geo_interface__ (self ):
251
+ coordinates = [self .x , self .y ]
252
+
253
+ if self .z is not None :
254
+ coordinates .append (self .z )
255
+
256
+ return {
257
+ 'id' : self .identifier ,
258
+ 'type' : 'Feature' ,
259
+ 'geometry' : point2geojsongeometry (self .x , self .y , self .z ),
260
+ 'properties' : {
261
+ 'name' : self .name ,
262
+ 'stn_type' : self .stn_type ,
263
+ 'gaw_id' : self .gaw_id ,
264
+ 'country' : self .country .country_name ,
265
+ # TODO: allow for contributor 1..n
266
+ 'wmo_region' : self .wmo_region ,
267
+ 'active_start_date' : self .active_start_date ,
268
+ 'active_end_date' : self .active_end_date
269
+ }
270
+ }
245
271
246
272
def __repr__ (self ):
247
273
return 'Station ({}, {})' .format (self .identifier , self .name )
@@ -276,8 +302,9 @@ class DataRecord(base):
276
302
instrument_model = Column (String , nullable = False )
277
303
instrument_number = Column (String , nullable = False )
278
304
279
- location = Column (Geometry (geometry_type = 'POINT' , srid = 4326 ),
280
- nullable = False )
305
+ x = Column (Float , nullable = False )
306
+ y = Column (Float , nullable = False )
307
+ z = Column (Float , nullable = True )
281
308
282
309
timestamp_utcoffset = Column (String , nullable = False )
283
310
timestamp_date = Column (Date , nullable = False )
@@ -341,9 +368,9 @@ def __init__(self, ecsv):
341
368
if 'Time' in ecsv .extcsv ['TIMESTAMP' ]:
342
369
self .timestamp_time = ecsv .extcsv ['TIMESTAMP' ]['Time' ]
343
370
344
- self .location = util . point2ewkt ( ecsv . extcsv ['LOCATION' ]['Longitude' ],
345
- ecsv .extcsv ['LOCATION' ]['Latitude' ],
346
- ecsv .extcsv ['LOCATION' ]['Height' ])
371
+ self .x = csv . extcsv ['LOCATION' ]['Longitude' ]
372
+ self . y = csv .extcsv ['LOCATION' ]['Latitude' ]
373
+ self . z = csv .extcsv ['LOCATION' ]['Height' ]
347
374
348
375
self .extcsv = ecsv .extcsv
349
376
self .raw = ecsv ._raw
@@ -387,35 +414,6 @@ def get_waf_path(self, basepath):
387
414
388
415
return '/' .join (url_tokens )
389
416
390
- def to_geojson_dict (self ):
391
- """return dict as a GeoJSON representation"""
392
-
393
- data = self .__dict__
394
-
395
- fields_to_remove = [
396
- '_sa_instance_state' ,
397
- 'extcsv' ,
398
- 'location' ,
399
- 'ingest_filepath' ,
400
- ]
401
-
402
- geometry = util .point2geojsongeometry (
403
- data ['extcsv' ]['LOCATION' ]['Longitude' ],
404
- data ['extcsv' ]['LOCATION' ]['Latitude' ],
405
- data ['extcsv' ]['LOCATION' ]['Height' ])
406
-
407
- LOGGER .debug ('removing internal / unwanted fields' )
408
- for field_to_remove in fields_to_remove :
409
- data .pop (field_to_remove , None )
410
-
411
- feature = {
412
- 'type' : 'Feature' ,
413
- 'geometry' : geometry ,
414
- 'properties' : data
415
- }
416
-
417
- return feature
418
-
419
417
def __repr__ (self ):
420
418
return 'DataRecord({}, {})' .format (self .identifier , self .url )
421
419
0 commit comments