1import json 2from jsonschema.validators import validator_for 3from .. import settings 4from ..type import Type 5 6 7class GeojsonType(Type): 8 """Geojson type implementation. 9 10 API | Usage 11 -------- | -------- 12 Public | `from frictionless import types` 13 14 """ 15 16 code = "geojson" 17 builtin = True 18 constraints = [ 19 "required", 20 "enum", 21 ] 22 23 # Read 24 25 def read_cell(self, cell): 26 if isinstance(cell, str): 27 try: 28 cell = json.loads(cell) 29 except Exception: 30 return None 31 if not isinstance(cell, dict): 32 return None 33 if self.field.format in ["default", "topojson"]: 34 try: 35 VALIDATORS[self.field.format].validate(cell) 36 except Exception: 37 return None 38 return cell 39 40 # Write 41 42 def write_cell(self, cell): 43 return json.dumps(cell) 44 45 46# Internal 47 48 49VALIDATORS = { 50 "default": validator_for(settings.GEOJSON_PROFILE)(settings.GEOJSON_PROFILE), 51 "topojson": validator_for(settings.TOPOJSON_PROFILE)(settings.TOPOJSON_PROFILE), 52} 53