def render_point(request, feature_id): """ Plot a point """ URL = "api/1.0/feature/%s.json" % (feature_id) r = query_georegistry(URL) d = json.loads(r) if d['status'] == 200: results = d['features'] latlon = results[0]['geometry']['coordinates'] geomtype = results[0]['geometry']['type'] lat = latlon[0] lon = latlon[1] properties = results[0]['properties'] proplist = [] property_type = properties['feature_type'] for k, v in properties.items(): i = "%s : %s" % (k, v) proplist.append(i) else: jsonstr = json.dumps(d, indent=4) return HttpResponse(jsonstr, status=d['status'], mimetype='application/json') return render_to_response( "point.html", { 'lat': lat, 'lon': lon, 'proplist': proplist, 'property_type': property_type }, context_instance=RequestContext(request), )
def render_point(request, feature_id): """ Plot a point """ URL="api/1.0/feature/%s.json" % (feature_id) r= query_georegistry(URL) d=json.loads(r) if d['status']==200: results= d['features'] latlon=results[0]['geometry']['coordinates'] geomtype=results[0]['geometry']['type'] lat=latlon[0] lon=latlon[1] properties=results[0]['properties'] proplist=[] property_type=properties['feature_type'] for k,v in properties.items(): i="%s : %s" % (k,v) proplist.append(i) else: jsonstr = json.dumps(d, indent=4) return HttpResponse(jsonstr, status=d['status'], mimetype='application/json') return render_to_response("point.html", {'lat': lat, 'lon': lon, 'proplist': proplist, 'property_type': property_type }, context_instance = RequestContext(request),)
def render_points(request, kwargs): """ Plot multiple points based on a kwarg dict """ data = urllib.urlencode(kwargs) URL = "api/1.0/features/search?%s" % (data) r = query_georegistry(URL) d = json.loads(r) if d['status'] == 200: pointslist = [] detaillist = [] for i in d['features']: c = 0 pointsitems = [] geomtype = i['geometry']['type'] if geomtype == "Point": formatted_properties = "" latlon = i['geometry']['coordinates'] lat = latlon[0] lon = latlon[1] properties = i['properties'] geomtype = i['geometry']['type'] for k, v in properties.items(): j = """%s=%s""" % (k, v) formatted_properties += j detaillist.append(formatted_properties) pointsitems.append(0) pointsitems.append(lat) pointsitems.append(lon) pointsitems.append(c) pointslist.append(pointsitems) c += 1 maplat = pointslist[0][1] maplon = pointslist[0][2] else: jsonstr = json.dumps(d, indent=4) return HttpResponse(jsonstr, status=d['status'], mimetype='application/json') return render_to_response( "multimarkers.html", { 'pointslist': pointslist, 'detaillist': detaillist, 'maplat': maplat, 'maplon': maplon, }, context_instance=RequestContext(request), )
def render_points(request, kwargs): """ Plot multiple points based on a kwarg dict """ data = urllib.urlencode(kwargs) URL="api/1.0/features/search?%s" % (data) r= query_georegistry(URL) d=json.loads(r) if d['status']==200: pointslist=[] detaillist=[] for i in d['features']: c=0 pointsitems=[] geomtype=i['geometry']['type'] if geomtype=="Point": formatted_properties="" latlon=i['geometry']['coordinates'] lat=latlon[0] lon=latlon[1] properties=i['properties'] geomtype=i['geometry']['type'] for k,v in properties.items(): j="""%s=%s""" % (k,v) formatted_properties+=j detaillist.append(formatted_properties) pointsitems.append(0) pointsitems.append(lat) pointsitems.append(lon) pointsitems.append(c) pointslist.append(pointsitems) c+=1 maplat=pointslist[0][1] maplon=pointslist[0][2] else: jsonstr = json.dumps(d, indent=4) return HttpResponse(jsonstr, status=d['status'], mimetype='application/json') return render_to_response("multimarkers.html", {'pointslist': pointslist, 'detaillist': detaillist, 'maplat': maplat, 'maplon': maplon, }, context_instance = RequestContext(request),)
def extract_lev2_data_from_shapfile(shape_path, country_code): """ Extracts county/LGA (level 2) data from a shapefile Requires: - A shape file path (without an extension) - A country code Returns: - A list of dicts containing the shapefile information """ dbf = "%s.dbf" % (shape_path) shp = "%s.shp" % (shape_path) URL = "/api/1.0/features/locations?country_code=%s" % (country_code) locations = json.loads(query_georegistry(URL)) if locations == {}: print "Warning: This country's heirchy is not in the georegistry." states = () else: country = locations.keys()[0] states = locations[country]['children'] l = [] driver = ogr.GetDriverByName('ESRI Shapefile') ds = driver.Open(dbf, 0) if ds is None: print 'Can not open', ds sys.exit(1) lyr = ds.GetLayer() totfeats = lyr.GetFeatureCount() lyr.SetAttributeFilter('') print 'Starting extraction of %s of %s features in shapefile %s...' % ( lyr.GetFeatureCount(), totfeats, lyr.GetName()) pbar = ProgressBar(maxval=lyr.GetFeatureCount()).start() k = 0 # iterate the features and access its attributes (including geometry) to store them in dict feat = lyr.GetNextFeature() while feat: d = {} geom = feat.GetGeometryRef() g = geom.ExportToJson() g = json.loads(g) d['geometry_coordinates'] = g['coordinates'][0] d['geometry_type'] = g['type'] # iterate the feature's fields to get its values and store them in dict feat_defn = lyr.GetLayerDefn() for i in range(feat_defn.GetFieldCount()): value = feat.GetField(i) if isinstance(value, str): value = unicode(value, 'latin-1') field = feat.GetFieldDefnRef(i) fieldname = field.GetName() d[fieldname] = value for i in states: if i['name'] == d['ADM1NAME']: #print i['name'], "MATCH!" d['subdivision_code'] = i['subdivision_code'] d['subdivision_slug'] = i['slug'] d['subdivision_name'] = d['ADM1NAME'] if d.has_key('ADM1NAME'): del d['ADM1NAME'] if d.has_key('ISO_Ctry'): d['country_code'] = d['ISO_Ctry'] del d['ISO_Ctry'] if d.has_key('Name'): d['name'] = d['Name'] del d['Name'] d['slug'] = slugify.slugify(unicode(d['name'])) borders = ds.GetLayerByName(lyr.GetName()) centroid = loads(feat.GetGeometryRef().ExportToWkb()) centroidpoint = centroid.representative_point()._get_coords()[0] d['geometry_centroid'] = list(centroidpoint) d['bounds'] = list(centroid.bounds) l.append(d) feat.Destroy() feat = lyr.GetNextFeature() k = k + 1 pbar.update(k) pbar.finish() return l
def fetch_country_data_from_gr(country_code): URL="/api/1.0/features/search?country_code=NG&limit=1000" r = query_georegistry(URL, gr_server, gr_user, gr_pass) return json.loads(r)
def extract_lev2_data_from_shapfile(shape_path, country_code): """ Extracts county/LGA (level 2) data from a shapefile Requires: - A shape file path (without an extension) - A country code Returns: - A list of dicts containing the shapefile information """ dbf="%s.dbf" % (shape_path) shp="%s.shp" % (shape_path) URL="/api/1.0/features/locations?country_code=%s" %(country_code) locations=json.loads(query_georegistry(URL)) if locations=={}: print "Warning: This country's heirchy is not in the georegistry." states=() else: country=locations.keys()[0] states=locations[country]['children'] l=[] driver = ogr.GetDriverByName('ESRI Shapefile') ds = driver.Open(dbf, 0) if ds is None: print 'Can not open', ds sys.exit(1) lyr = ds.GetLayer() totfeats = lyr.GetFeatureCount() lyr.SetAttributeFilter('') print 'Starting extraction of %s of %s features in shapefile %s...' % (lyr.GetFeatureCount(), totfeats, lyr.GetName()) pbar = ProgressBar(maxval=lyr.GetFeatureCount()).start() k=0 # iterate the features and access its attributes (including geometry) to store them in dict feat = lyr.GetNextFeature() while feat: d={} geom = feat.GetGeometryRef() g = geom.ExportToJson() g=json.loads(g) d['geometry_coordinates']= g['coordinates'][0] d['geometry_type']=g['type'] # iterate the feature's fields to get its values and store them in dict feat_defn = lyr.GetLayerDefn() for i in range(feat_defn.GetFieldCount()): value = feat.GetField(i) if isinstance(value, str): value = unicode(value, 'latin-1') field = feat.GetFieldDefnRef(i) fieldname = field.GetName() d[fieldname] = value for i in states: if i['name']==d['ADM1NAME']: #print i['name'], "MATCH!" d['subdivision_code']=i['subdivision_code'] d['subdivision_slug']=i['slug'] d['subdivision_name']=d['ADM1NAME'] if d.has_key('ADM1NAME'): del d['ADM1NAME'] if d.has_key('ISO_Ctry'): d['country_code']=d['ISO_Ctry'] del d['ISO_Ctry'] if d.has_key('Name'): d['name']=d['Name'] del d['Name'] d['slug']=slugify.slugify(unicode(d['name'])) borders = ds.GetLayerByName(lyr.GetName()) centroid=loads(feat.GetGeometryRef().ExportToWkb()) centroidpoint = centroid.representative_point()._get_coords()[0] d['geometry_centroid']=list(centroidpoint) d['bounds']=list(centroid.bounds) l.append(d) feat.Destroy() feat = lyr.GetNextFeature() k = k + 1 pbar.update(k) pbar.finish() return l