<s> import decimal <EOL> import json <EOL> import tempfile <EOL> import geojson <EOL> import ijson <EOL> import jsonpath_rw <EOL> def jsonObjectReader ( filepath ) : <EOL> '''<STR_LIT>''' <EOL> top_level_array = False <EOL> array_stack = <NUM_LIT:0> <EOL> top_level_object = False <EOL> object_stack = <NUM_LIT:0> <EOL> parser = ijson . parse ( open ( filepath , '<STR_LIT:r>' ) ) <EOL> for prefix , event , value in parser : <EOL> if event == '<STR_LIT>' : <EOL> if not top_level_array : <EOL> top_level_array = True <EOL> continue <EOL> else : <EOL> array_stack += <NUM_LIT:1> <EOL> if event == '<STR_LIT>' : <EOL> if not top_level_object : <EOL> top_level_object = True <EOL> builder = ijson . ObjectBuilder ( ) <EOL> else : <EOL> object_stack += <NUM_LIT:1> <EOL> if event == '<STR_LIT>' : <EOL> if not top_level_object : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> else : <EOL> if object_stack == <NUM_LIT:0> : <EOL> top_level_object = False <EOL> yield builder . value <EOL> else : <EOL> object_stack -= <NUM_LIT:1> <EOL> if event == '<STR_LIT>' : <EOL> if not top_level_array : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> else : <EOL> if array_stack == <NUM_LIT:0> : <EOL> top_level_array = False <EOL> else : <EOL> array_stack -= <NUM_LIT:1> <EOL> if isinstance ( value , decimal . Decimal ) : <EOL> value = float ( value ) <EOL> builder . event ( event , value ) <EOL> def jsonArrayHead ( filepath , limit = <NUM_LIT:10> ) : <EOL> '''<STR_LIT>''' <EOL> reader = jsonObjectReader ( filepath ) <EOL> objs = [ ] <EOL> objCount = <NUM_LIT:0> <EOL> for obj in reader : <EOL> if objCount == limit : <EOL> break <EOL> objs . append ( obj ) <EOL> objCount += <NUM_LIT:1> <EOL> return objs <EOL> class JsonMapper ( object ) : <EOL> def __init__ ( self , objConverter , header = '<STR_LIT:[>' , footer = '<STR_LIT:]>' , <EOL> jsonDumpser = json . dumps ) : <EOL> self . objConverter = objConverter <EOL> self . header = header <EOL> self . footer = footer <EOL> self . jsonDumpser = json . dumps <EOL> def mapToJsonFile ( self , tmpdir , objects , outFilepath = None ) : <EOL> if not outFilepath : <EOL> outFilepath = tempfile . mkstemp ( suffix = '<STR_LIT>' , dir = tmpdir ) [ <NUM_LIT:1> ] <EOL> writer = open ( outFilepath , '<STR_LIT:w>' ) <EOL> self . mapToJson ( objects , writer ) <EOL> writer . close ( ) <EOL> return outFilepath <EOL> def mapToJson ( self , objects , writer ) : <EOL> writer . write ( self . header ) <EOL> writer . write ( '<STR_LIT:\n>' ) <EOL> for ind , obj in enumerate ( objects ) : <EOL> if ind > <NUM_LIT:0> : <EOL> writer . write ( '<STR_LIT>' ) <EOL> else : <EOL> writer . write ( '<STR_LIT:\n>' ) <EOL> writer . write ( self . jsonDumpser ( self . objConverter ( obj ) ) ) <EOL> writer . write ( self . footer ) <EOL> class GeoJsonMapper ( JsonMapper ) : <EOL> def __init__ ( self , objConverter = None , mapping = None ) : <EOL> geojson_header = """<STR_LIT>""" <EOL> geojson_footer = """<STR_LIT>""" <EOL> if objConverter is None : <EOL> if mapping is None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def convertToGeoJson ( obj ) : <EOL> lat_expr = jsonpath_rw . parse ( mapping [ '<STR_LIT>' ] ) <EOL> long_expr = jsonpath_rw . parse ( mapping [ '<STR_LIT>' ] ) <EOL> def extractLat ( obj ) : <EOL> match = lat_expr . find ( obj ) <EOL> return float ( match [ <NUM_LIT:0> ] . value ) <EOL> def extractLong ( obj ) : <EOL> match = long_expr . find ( obj ) <EOL> return float ( match [ <NUM_LIT:0> ] . value ) <EOL> point = geojson . Point ( ( extractLong ( obj ) , extractLat ( obj ) ) ) <EOL> properties = { "<STR_LIT>" : <NUM_LIT:0> } <EOL> feature = geojson . Feature ( geometry = point , <EOL> properties = properties ) <EOL> return feature <EOL> objConverter = convertToGeoJson <EOL> super ( GeoJsonMapper , self ) . __init__ ( objConverter , geojson_header , <EOL> geojson_footer , geojson . dumps ) </s>
