def parse_tabular(response, tablename, tablehash): """ Open a table containing tabular data and return a listing of fields """ if tablehash in seen_classes: cls = current_app.class_references[tablehash] else: db.metadata.reflect(bind=db.engine) seen_classes.add(tablehash) #Dynamic class creation using metaclasses geomtype = "Polygon" basegeomcls = GEOMLOOKUP[geomtype] cls = type( str(tablehash), ( basegeomcls, db.Model, ), { '__tablename__': tablehash, '__table_args__': { 'extend_existing': True } }) current_app.class_references[tablehash] = cls response['data']['name'] = tablename response['data']['fields'] = [c.name for c in cls.__table__.columns] response['data']['fields'].append('geojson') #TODO: Add topojson support if the db is postgresql return response
def get_dataset_field(tablename, field): response = {'status': 'success', 'data': {}} if tablename in seen_classes: cls = current_app.class_references[tablename] else: db.metadata.reflect(bind=db.engine) seen_classes.add(tablename) cls = type( str(tablename), ( GeoPoly, db.Model, ), { '__tablename__': tablename, '__table_args__': { 'extend_existing': True } }) current_app.class_references[tablename] = cls if field == config.geom_column: vector = cls.query.with_entities( geofuncs.ST_AsGeoJSON(getattr(cls, field))).all() response['data'] = [v[0] for v in vector] elif field == 'geojson': #TODO: How can this be cleaner? Do I need 2 queries go get geojson? #rows = cls.query.all() geoms = cls.query.with_entities( geofuncs.ST_AsGeoJSON(getattr(cls, config.geom_column))).all() features = [] for i, row in enumerate(geoms): #attributes = row.as_dict() #attributes.pop('wkb_geometry', None) #for k, v in attributes.iteritems(): #if isinstance(v, decimal.Decimal): #attributes[k] = float(v) current_feature = { 'type': 'Feature', 'geometry': ast.literal_eval(geoms[i][0]) } #'properties':attributes} features.append(current_feature) geojson = {"type": "FeatureCollection", "features": features} #geojson = {"type":"FeatureCollection", "features": geoms} response['data']['geojson'] = geojson elif field == 'topojson': #TODO: Add topojson support if the DB is postgresql pass else: vector = cls.query.with_entities(getattr(cls, field)).all() responsevector = [v[0] for v in vector] if isinstance(responsevector[0], decimal.Decimal): for i, v in enumerate(responsevector): responsevector[i] = float(v) response['data'] = responsevector return response
def get_dataset_field(tablename, field): response = {'status':'success','data':{}} if tablename in seen_classes: cls = current_app.class_references[tablename] else: db.metadata.reflect(bind=db.engine) seen_classes.add(tablename) cls = type(str(tablename), (GeoPoly, db.Model,), {'__tablename__':tablename, '__table_args__' : {'extend_existing': True}}) current_app.class_references[tablename] = cls if field == config.geom_column: vector = cls.query.with_entities(geofuncs.ST_AsGeoJSON(getattr(cls, field))).all() response['data'] = [v[0] for v in vector] elif field == 'geojson': #TODO: How can this be cleaner? Do I need 2 queries go get geojson? #rows = cls.query.all() geoms = cls.query.with_entities(geofuncs.ST_AsGeoJSON(getattr(cls, config.geom_column))).all() features = [] for i, row in enumerate(geoms): #attributes = row.as_dict() #attributes.pop('wkb_geometry', None) #for k, v in attributes.iteritems(): #if isinstance(v, decimal.Decimal): #attributes[k] = float(v) current_feature = {'type':'Feature', 'geometry':ast.literal_eval(geoms[i][0])} #'properties':attributes} features.append(current_feature) geojson = {"type": "FeatureCollection","features": features} #geojson = {"type":"FeatureCollection", "features": geoms} response['data']['geojson'] = geojson elif field == 'topojson': #TODO: Add topojson support if the DB is postgresql pass elif field == 'raw': return {'status':'error', 'message':'Tabular data does not have a raw representation, yet.'} else: vector = cls.query.with_entities(getattr(cls, field)).all() responsevector = [v[0] for v in vector] if isinstance(responsevector[0], decimal.Decimal): for i, v in enumerate(responsevector): responsevector[i] = float(v) response['data'] = responsevector return response
def parse_tabular(response, tablename, tablehash): """ Open a table containing tabular data and return a listing of fields """ if tablehash in seen_classes: cls = current_app.class_references[tablehash] else: db.metadata.reflect(bind=db.engine) seen_classes.add(tablehash) #Dynamic class creation using metaclasses geomtype = "Polygon" basegeomcls = GEOMLOOKUP[geomtype] cls = type(str(tablehash), (basegeomcls, db.Model,), {'__tablename__':tablehash, '__table_args__' : {'extend_existing': True}}) current_app.class_references[tablehash] = cls response['data']['name'] = tablename response['data']['fields'] = [c.name for c in cls.__table__.columns] response['data']['fields'].append('geojson') #TODO: Add topojson support if the db is postgresql return response