Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

#!/usr/bin/env python 

# -*- coding: utf-8 -*- 

 

############################################################################### 

#  Copyright Kitware Inc. 

# 

#  Licensed under the Apache License, Version 2.0 ( the "License" ); 

#  you may not use this file except in compliance with the License. 

#  You may obtain a copy of the License at 

# 

#    http://www.apache.org/licenses/LICENSE-2.0 

# 

#  Unless required by applicable law or agreed to in writing, software 

#  distributed under the License is distributed on an "AS IS" BASIS, 

#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

#  See the License for the specific language governing permissions and 

#  limitations under the License. 

############################################################################### 

 

""" 

This module contains helper classes for transforming arbitrary objects into 

geoJSON objects.  The intended usage is to iterate on a mongo cursor to 

construct feature collections.  For example, a girder plugin can transform 

an item query into a geoJSON object of point features.  Assuming items 

are stored as follows: 

 

    { 

        "meta": { 

            "lat": *, 

            "lon": *, 

            "someproperty": * 

        } 

    } 

 

This module can be used as follows with cursor returned by a mongo query 

over some girder items: 

 

    >>> pointSpec = { \ 

            "latitude": "meta.lat", \ 

            "longitude": "meta.lon", \ 

            "keys": ["meta"], \ 

            "flatten": ["meta"] \ 

        } 

    >>> collection = FeatureCollection(myPoints=pointSpec) 

    >>> obj = features(myPoints=cursor) 

 

The resulting object will contain an array of point features with properties 

set to the item metadata as follows: 

 

    { 

        "type": "FeatureCollection", 

        "features": [ 

            { 

                "geometry": { 

                    "type": "Point", 

                    "coordinates": [ 

                        *, 

                        * 

                    ] 

                }, 

                "type": "Feature", 

                "properties": { 

                    "lat": *, 

                    "lon": *, 

                    "someproperty": * 

                } 

            }, 

            ... 

        ] 

    } 

""" 

 

 

class GeoJSONException(Exception): 

    """ 

    Base exception class for all errors raised by this module. 

    """ 

    pass 

 

 

class IteratorException(GeoJSONException): 

    """ 

    This exception is raised when a method cannot iterate over 

    an expected iterable argument. 

    """ 

    pass 

 

 

class AccessorException(GeoJSONException): 

    """ 

    This exception is raised when an invalid accessor is encountered. 

    """ 

    pass 

 

 

class InvalidKeyException(GeoJSONException): 

    """ 

    This exception is raised when trying to access an invalid property 

    via an accessor. 

    """ 

    pass 

 

 

class AbstractMethodException(GeoJSONException): 

    """ 

    This exception is raised when an abstract method or class is 

    called. 

    """ 

    pass 

 

 

class BadSpecException(GeoJSONException): 

    """ 

    This exception is raised by a feature class when it is passed 

    an invalid spec object. 

    """ 

    pass 

 

 

class Base(object): 

    """ 

    Base class for all classes in this module. 

    """ 

 

    def __call__(self, *arg, **kw):  # pragma: no cover 

        """ 

        The call method is used by subclasses to transform 

        and argument or iterable into a specific geoJSON 

        component.  It must be overriden by subclasses. 

        """ 

        raise AbstractMethodException 

 

    @classmethod 

    def map(cls, func, data): 

        """ 

        Call the method `func` on every element of the iterable 

        `data`.  Returns a list of the returned results. 

        """ 

        try: 

            iter(data) 

        except TypeError: 

            raise IteratorException( 

                "Expected an iterable but got a '%s'" % type(data) 

            ) 

        return [func(d, i, data) for i, d in enumerate(data)] 

 

    @classmethod 

    def get(cls, acc, data): 

        """ 

        Get a property of a data elemennt determined by an accessor. 

 

        The data objects should implement the [] operator like a 

        `dict` or `list`. 

 

        The accessor can either be a string or an int.  If it is an 

        int then `data[acc]` is returned.  If it is a string and 

        the data type is a tuple or list, then it is coerced into 

        an int. 

 

        The accessor can also reference subobjects by using a '.' 

        to seperate keys.  For example: 

 

            >>> Base.get('a.b.1', {'a': {'b': [10, 11]}}) 

            11 

        """ 

        if acc is None: 

            return data 

        if isinstance(acc, basestring): 

            splt = acc.split('.') 

            if len(splt) > 1: 

                d = data 

                for a in splt: 

                    d = cls.get(a, d) 

                return d 

 

            if isinstance(data, list) or isinstance(data, tuple): 

                try: 

                    acc = int(acc) 

                except ValueError: 

                    pass 

 

        try: 

            value = data[acc] 

        except TypeError: 

            raise AccessorException( 

                "Invalid data type '%s' for accessor '%s'" % 

                (type(data), acc) 

            ) 

        except (KeyError, IndexError): 

            raise InvalidKeyException( 

                "Property '%s' not found in data" % str(acc) 

            ) 

        return value 

 

 

class Position(Base): 

    """ 

    This class formats GIS coordinates into a geoJSON position array. 

    """ 

    def __init__(self, longitude=0, latitude=1, **kw): 

        """ 

        Initialize the Position object with accessors to the longitude 

        and latitude. 

        """ 

        self.longitude = longitude 

        self.latitude = latitude 

 

    def __call__(self, data): 

        """ 

        Convert an object to a geoJSON position array. 

        """ 

        lon = self.get(self.longitude, data) 

        lat = self.get(self.latitude, data) 

        return (lon, lat) 

 

 

# more geoJSON coordinate types to be implemented 

class PositionArray(Position): 

    """unimplemented""" 

    pass 

 

 

class LinearRing(PositionArray): 

    """unimplemented""" 

    pass 

 

 

class MultiPositionArray(PositionArray): 

    """unimplemented""" 

    pass 

 

 

class MultiLinearRing(LinearRing): 

    """unimplemented""" 

    pass 

 

 

class Geometry(Base): 

    """ 

    Base class for generating geoJSON geometries. 

    """ 

    typeName = None    # geoJSON name for the geometry 

    coordinate = None  # coordinate generating class 

 

    def __init__(self, **kw): 

        """ 

        Construct a Geometry object.  Keyword arguments are passed 

        to the coordinate class associated with the geometry. 

        """ 

        if self.coordinate is None:  # pragma: no cover 

            raise AbastractMethodException 

 

        self.coord = self.coordinate(**kw) 

 

    def __call__(self, d, *args): 

        """ 

        Convert an object into a geoJSON geometry. 

        """ 

        if self.coordinate is None:  # pragma: no cover 

            raise AbstractMethodException 

 

        return { 

            'coordinates': self.coord(d), 

            'type': self.typeName 

        } 

 

 

class Point(Geometry): 

    """ 

    A geoJSON Point geometry generator class. 

    """ 

    typeName = 'Point' 

    coordinate = Position 

 

 

# more geoJSON geometries to be implemented 

class LineString(Geometry): 

    """unimplemented""" 

    pass 

 

 

class MultiPoint(Geometry): 

    """unimplemented""" 

    pass 

 

 

class Polygon(Geometry): 

    """unimplemented""" 

    pass 

 

 

class Feature(Base): 

    """ 

    A base class for generating geoJSON features. 

    """ 

    typeName = 'Feature'  # feature type string 

    geometry = None       # geometry associated with this feature 

 

    def __init__(self, keys=None, flatten=[], **kw): 

        """ 

        Initialize the feature object.  Extra keyword arguments are passed 

        to the associated geometry class. 

 

        :param keys: The keys from the object to add as properties to the 

                     geoJSON feature.  If this is parameter is None, then 

                     all keys are used. 

        :type keys: [str] 

        :param flatten: An array of keys that map to objects that will be 

                        used to extend the properties.  This is used to 

                        flatten hierarchical objects into a single object. 

        :type flatten: [str] 

        """ 

        if self.geometry is None:  # pragma: no cover 

            raise AbstractMethodException 

 

        self._geometry = self.geometry(**kw) 

        self.keys = keys 

        self.flatten = flatten 

 

    @classmethod 

    def filter(cls, d, keys): 

        """ 

        Return a copy of the object `d` with only the given keys. 

        """ 

        return {k: d[k] for k in d if k in keys} 

 

    @classmethod 

    def flat(cls, d, key): 

        """ 

        Remove `key` from `d` and extend it by the dictionary referenced 

        by it. 

        """ 

        d.update(d.pop(key, {})) 

        return d 

 

    def __call__(self, d, *arg): 

        """ 

        Convert an object into a geoJSON feature. 

        """ 

        if self.geometry is None:  # pragma: no cover 

            raise AbstractMethodException 

 

        keys = self.keys 

        if keys is None: 

            keys = d.keys() 

        meta = self.filter(d, keys) 

 

        for key in self.flatten: 

            self.flat(meta, key) 

 

        return { 

            'type': self.typeName, 

            'properties': meta, 

            'geometry': self._geometry(d) 

        } 

 

 

class PointFeature(Feature): 

    """ 

    A geoJSON point feature generator class. 

    """ 

    geometry = Point 

 

 

class FeatureCollection(Base): 

    """ 

    A geoJSON feature collection generator class. 

    """ 

    typeName = 'FeatureCollection' 

    features = {  # dictionary of all known feature types 

        'point': PointFeature 

    } 

 

    @classmethod 

    def getFeatureClass(cls, name): 

        """ 

        Get the actual class for a given feature name string. 

        """ 

        try: 

            return cls.features[name] 

        except KeyError:  # pragma: no cover 

            raise InvalidKeyException('Unknown feature type "%s"' % name) 

 

    def __init__(self, **features): 

        """ 

        Initialize a FeatureCollection object. 

        :param features: A dictionary of specifications for feature 

                         conversions.  Each key represents a named 

                         specification that maps to a dictionary of 

                         the form {'type': featureType, ...}. 

                         Additional values in this dictionary are passed as 

                         keyword arguments to the feature constructor. 

        :type features: dict 

        """ 

        self._features = {} 

        for name in features: 

            self.addFeatureSpec(name, features[name]) 

 

    def addFeatureSpec(self, name, spec): 

        """ 

        Add a feature specification to the collection. 

        """ 

        spec = dict(spec) 

        typeName = spec.pop('type', None) 

        featureCls = self.getFeatureClass(typeName) 

        self._features[name] = featureCls(**spec) 

 

    def __call__(self, **kw): 

        """ 

        Convert one or more iterables into geoJSON feature collection. 

        Iterable datasets should be passed as keyword arguments to 

        this method.  The argument name must be a feature defined 

        through the constructor or added later by addFeatureSpec. 

        """ 

        features = [] 

 

        for specName in kw: 

            feature = self._features[specName] 

            features.extend(self.map(feature, kw[specName])) 

 

        return { 

            'type': self.typeName, 

            'features': features 

        }