def hully(g_list): from django.contrib.gis.geos import GEOSGeometry from django.contrib.gis.geos import MultiPoint from django.contrib.gis.geos import GeometryCollection # maybe mixed bag types = list(set([g['type'] for g in g_list])) # should work for any combination try: hull = GeometryCollection( [GEOSGeometry(json.dumps(g)) for g in g_list]).convex_hull except: print('hully() failed on g_list', g_list) # buffer points, but only a little if near meridian if len(types) == 1 and types[0] == 'Point': l = list(set([g_list[0]['coordinates'][0] for c in g_list[0]])) if len([i for i in l if i >= 175]) == 0: hull = hull.buffer(1) else: hull = hull.buffer(0.1) return json.loads(hull.geojson) if hull.geojson != None else []
def vector_catalog_translate_features(request, old_layer, new_layer): first = timeit.timeit() features = VECTOR_LAYERS[old_layer]['geometries_by_id']( request.data['features']) geometries = [g for g, props in features] second = timeit.timeit() union = GeometryCollection(*geometries).unary_union bbox = union.envelope import math unit = math.sqrt(bbox.area) / 40 simpler = union.buffer(unit) # print bbox, unit, union.area, len(union.wkt), len(simpler.wkt) third = timeit.timeit() response = Response(VECTOR_LAYERS[new_layer]['features_by_wkt']( simpler.wkt # bbox.wkt )) fourth = timeit.timeit() print "retrieve geoms: ", second - first print "union: ", third - second print "retrieve features: ", fourth - third return response