def report_json(request): try: division_code = request.matchdict.get('divisioncode') except: raise HTTPBadRequest(detail='incorrect value for parameter ' '"divisioncode"') try: resolution = float(request.params.get('resolution')) except: raise HTTPBadRequest(detail='invalid value for parameter "resolution"') hazard_type = request.matchdict.get('hazardtype', None) _filter = or_(AdministrativeDivision.code == division_code, AdministrativeDivision.parent_code == division_code) simplify = func.ST_Simplify( func.ST_Transform(AdministrativeDivision.geom, 3857), resolution / 2) if hazard_type is not None: divisions = DBSession.query(AdministrativeDivision) \ .add_columns(simplify, HazardLevel.mnemonic, HazardLevel.title) \ .outerjoin(AdministrativeDivision.hazardcategories) \ .join(HazardCategory) \ .outerjoin(HazardType)\ .outerjoin(HazardLevel) \ .filter(and_(_filter, HazardType.mnemonic == hazard_type)) else: divisions = DBSession.query(AdministrativeDivision) \ .add_columns(simplify, literal_column("'None'"), literal_column("'blah'")) \ .filter(_filter) return [{ 'type': 'Feature', 'geometry': to_shape(geom_simplified), 'properties': { 'name': division.name, 'code': division.code, 'hazardLevelMnemonic': hazardlevel_mnemonic, 'hazardLevelTitle': hazardlevel_title } } for division, geom_simplified, hazardlevel_mnemonic, hazardlevel_title in divisions]
def report_neighbours_geojson(request): try: division_code = request.matchdict.get('divisioncode') except: raise HTTPBadRequest(detail='incorrect value for parameter ' '"divisioncode"') try: resolution = float(request.params.get('resolution')) except: raise HTTPBadRequest(detail='invalid value for parameter "resolution"') try: bbox = request.params.get('bbox') box = [float(x) for x in bbox.split(',')] bbox = Polygon(((box[0], box[1]), (box[0], box[3]), (box[2], box[3]), (box[2], box[1]), (box[0], box[1]))) bbox = from_shape(bbox, srid=4326) except: raise HTTPBadRequest(detail='invalid value for parameter "bbox"') simplify = func.ST_Simplify( func.ST_Transform(AdministrativeDivision.geom, 3857), resolution) division = get_division(division_code) divisions = DBSession.query(AdministrativeDivision) \ .add_columns(simplify) \ .filter(func.ST_DWITHIN(AdministrativeDivision.geom, bbox, 0)) \ .filter(AdministrativeDivision.leveltype_id == division.leveltype_id) return [{ 'type': 'Feature', 'geometry': to_shape(geom_simplified), 'properties': { 'name': div.name, 'code': div.code, 'url': request.route_url('report_overview', division=div) } } for div, geom_simplified in divisions]
def add_dummy_communities(): try: logger.info(f"Adding dummy communities") with session_scope() as session: if session.query(Node).count() > 0: logger.info("Nodes not empty, not adding dummy communities") return with open("src/data/dummy_communities.json", "r") as file: data = json.loads(file.read()) for community in data["communities"]: geom = None if "coordinates" in community: geom = create_polygon_lng_lat(community["coordinates"]) elif "osm_id" in community: with open(f"src/data/osm/{community['osm_id']}.geojson" ) as f: geojson = json.loads(f.read()) # pick the first feature geom = geojson_to_geom(geojson["features"][0]["geometry"]) if "geom_simplify" in community: geom = func.ST_Simplify(geom, community["geom_simplify"], True) else: ValueError("No geom or osm_id specified for node") name = community["name"] admins = session.query(User).filter( User.username.in_(community["admins"])).all() members = session.query(User).filter( User.username.in_(community["members"])).all() parent_name = community["parent"] if parent_name: parent_node = (session.query(Node).join( Cluster, Cluster.parent_node_id == Node.id).filter( Cluster.is_official_cluster).filter( Cluster.name == community["parent"]).one()) node = Node( geom=to_multi(geom), parent_node=parent_node if parent_name else None, ) session.add(node) cluster = Cluster( name=f"{name}", description=f"Description for {name}", parent_node=node, is_official_cluster=True, ) session.add(cluster) main_page = Page( parent_node=node, creator_user=admins[0], owner_cluster=cluster, type=PageType.main_page, thread=Thread(), ) session.add(main_page) page_version = PageVersion( page=main_page, editor_user=admins[0], title=f"Main page for the {name} community", content="There is nothing here yet...", ) session.add(page_version) for admin in admins: cluster.cluster_subscriptions.append( ClusterSubscription( user=admin, role=ClusterRole.admin, )) for member in members: cluster.cluster_subscriptions.append( ClusterSubscription( user=member, role=ClusterRole.member, )) for group in data["groups"]: name = group["name"] admins = session.query(User).filter( User.username.in_(group["admins"])).all() members = session.query(User).filter( User.username.in_(group["members"])).all() parent_node = (session.query(Node).join( Cluster, Cluster.parent_node_id == Node.id).filter( Cluster.is_official_cluster).filter( Cluster.name == group["parent"]).one()) cluster = Cluster( name=f"{name}", description=f"Description for the group {name}", parent_node=parent_node, ) session.add(cluster) main_page = Page( parent_node=cluster.parent_node, creator_user=admins[0], owner_cluster=cluster, type=PageType.main_page, thread=Thread(), ) session.add(main_page) page_version = PageVersion( page=main_page, editor_user=admins[0], title=f"Main page for the {name} group", content="There is nothing here yet...", ) session.add(page_version) for admin in admins: cluster.cluster_subscriptions.append( ClusterSubscription( user=admin, role=ClusterRole.admin, )) for member in members: cluster.cluster_subscriptions.append( ClusterSubscription( user=member, role=ClusterRole.member, )) for place in data["places"]: owner_cluster = session.query(Cluster).filter( Cluster.name == place["owner"]).one() creator = session.query(User).filter( User.username == place["creator"]).one() page = Page( parent_node=owner_cluster.parent_node, creator_user=creator, owner_cluster=owner_cluster, type=PageType.place, thread=Thread(), ) session.add(page) page_version = PageVersion( page=page, editor_user=creator, title=place["title"], content=place["content"], address=place["address"], geom=create_coordinate(place["coordinate"][1], place["coordinate"][0]), ) session.add(page_version) for guide in data["guides"]: owner_cluster = session.query(Cluster).filter( Cluster.name == guide["owner"]).one() creator = session.query(User).filter( User.username == guide["creator"]).one() page = Page( parent_node=owner_cluster.parent_node, creator_user=creator, owner_cluster=owner_cluster, type=PageType.guide, thread=Thread(), ) session.add(page) page_version = PageVersion( page=page, editor_user=creator, title=guide["title"], content=guide["content"], geom=create_coordinate(guide["coordinate"][1], guide["coordinate"][0]) if "coordinate" in guide else None, ) session.add(page_version) except IntegrityError: logger.error( "Failed to insert dummy communities, are they already inserted?")
def report_geojson(request): division_code = request.matchdict.get('divisioncode') try: resolution = float(request.params.get('resolution')) except: raise HTTPBadRequest(detail='invalid value for parameter "resolution"') hazard_type = request.matchdict.get('hazardtype', None) ''' Here we want to address the "small islands" issue. We first check how big the polygons are compared to map resolution. ''' area = DBSession.query(func.ST_Area( func.ST_Transform(AdministrativeDivision.geom, 3857))) \ .filter(AdministrativeDivision.code == division_code) \ .scalar() if area < math.pow(resolution, 2) * 1000: # Simplify a bit first to prevent buffer to be too expensive # Here '1000000' is an empiric value. It works for Maldives without # generating empty geometries first_simplify = func.ST_Simplify( func.ST_Transform(AdministrativeDivision.geom, 3857), area / 1000000) # make the polygons a bit bigger geom = func.ST_Buffer(first_simplify, resolution * 2) else: geom = func.ST_Transform(AdministrativeDivision.geom, 3857) simplify = func.ST_Simplify(geom, resolution / 2) _filter = or_(AdministrativeDivision.code == division_code, AdministrativeDivision.parent_code == division_code) if hazard_type is not None: divisions = DBSession.query(AdministrativeDivision) \ .add_columns(simplify, HazardLevel.mnemonic, HazardLevel.title) \ .outerjoin(AdministrativeDivision.hazardcategories) \ .join(HazardCategory) \ .join(HazardType)\ .join(HazardLevel) \ .filter(and_(_filter, HazardType.mnemonic == hazard_type)) else: divisions = DBSession.query(AdministrativeDivision) \ .add_columns(simplify, literal_column("'None'"), literal_column("'blah'")) \ .filter(_filter) return [{ 'type': 'Feature', 'geometry': to_shape(geom_simplified), 'properties': { 'name': division.name, 'code': division.code, 'url': request.route_url('report_overview', division=division), 'hazardLevelMnemonic': hazardlevel_mnemonic, 'hazardLevelTitle': hazardlevel_title } } for division, geom_simplified, hazardlevel_mnemonic, hazardlevel_title in divisions]