/
geojson_reader.py
104 lines (82 loc) · 3.01 KB
/
geojson_reader.py
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
from __future__ import absolute_import, division, print_function
from builtins import (
bytes, str, open, super, range, zip, round, input, int, pow, object
)
import re
from six import string_types
import geopandas
from gaia.io.readers import GaiaReader
from gaia.gaia_data import GaiaDataObject
from gaia import GaiaException
from gaia.util import (
MissingParameterError,
MissingDataException,
UnsupportedFormatException,
get_uri_extension
)
import gaia.formats as formats
import gaia.types as types
class GaiaGeoJSONReader(GaiaReader):
"""
Another specific subclass for reading GeoJSON
"""
epsgRegex = re.compile('epsg:([\d]+)')
def __init__(self, url, *args, **kwargs):
super(GaiaGeoJSONReader, self).__init__(*args, **kwargs)
self.uri = url
self.ext = '.%s' % get_uri_extension(self.uri)
@staticmethod
def can_read(url, *args, **kwargs):
# Todo update for girder-hosted files
if not isinstance(url, string_types):
return False
extension = '.{}'.format(get_uri_extension(url))
if extension in formats.VECTOR:
return True
return False
def read(self, format=None, epsg=None):
return super().read(format, epsg)
def load_metadata(self, dataObject):
self.__read_internal(dataObject)
def load_data(self, dataObject):
self.__read_internal(dataObject)
def __read_internal(self, dataObject):
# FIXME: need to handle format
# if not self.format:
# self.format = self.default_output
# FIXME: Should this check actually go into the can_read method?
if self.ext not in formats.VECTOR:
raise UnsupportedFormatException(
"Only the following vector formats are supported: {}".format(
','.join(formats.VECTOR)
)
)
data = geopandas.read_file(self.uri)
# FIXME: still need to handle filtering
# if self.filters:
# self.filter_data()
# FIXME: skipped the transformation step for now
# return self.transform_data(format, epsg)
# do the actual reading and set both data and metadata
# on the dataObject parameter
# Initialize metadata
metadata = dict()
# Calculate bounds
feature_bounds = data.bounds
minx = feature_bounds['minx'].min()
miny = feature_bounds['miny'].min()
maxx = feature_bounds['maxx'].max()
maxy = feature_bounds['maxy'].max()
# Hack format to match resonant geodata (geojson polygon)
coords = [[
[minx, miny], [], [maxx, maxy], []
]]
metadata['bounds'] = dict(coordinates=coords)
dataObject.set_metadata(metadata)
dataObject.set_data(data)
epsgString = data.crs['init']
m = self.epsgRegex.search(epsgString)
if m:
dataObject._epsg = int(m.group(1))
dataObject._datatype = types.VECTOR
dataObject._dataformat = formats.VECTOR