Example #1
0
 def with_properties(self):
     if request.args.get('bounds') is None:
         return Response(json.dumps([]), mimetype='application/json')
     bounds_parts = request.args.get("bounds").split(',')
     points = Point.query.filter(func.ST_Contains(func.ST_MakeEnvelope(bounds_parts[1], bounds_parts[0], bounds_parts[3], bounds_parts[2]), Point.geom)).filter(Point.approved).all()
     points = list(map(lambda point: point.serialize_with_properties(), points))
     return Response(json.dumps(points),  mimetype='application/json')
Example #2
0
    def _proto_read(self, layer):
        """ Read features for the layer based on the self.request. """
        proto = self._get_protocol_for_layer(layer)
        if layer.public:
            return proto.read(self.request)
        user = self.request.user
        if user is None:
            raise HTTPForbidden()
        cls = proto.mapped_class
        geom_attr = proto.geom_attr
        ras = models.DBSession.query(RestrictionArea.area, RestrictionArea.area.ST_SRID())
        ras = ras.join(RestrictionArea.roles)
        ras = ras.join(RestrictionArea.layers)
        ras = ras.filter(Role.id == user.role.id)
        ras = ras.filter(Layer.id == layer.id)
        collect_ra = []
        use_srid = -1
        for ra, srid in ras.all():
            if ra is None:
                return proto.read(self.request)
            else:
                use_srid = srid
                collect_ra.append(to_shape(ra))
        if len(collect_ra) == 0:  # pragma: no cover
            raise HTTPForbidden()

        filter1_ = create_filter(self.request, cls, geom_attr)
        ra = cascaded_union(collect_ra)
        filter2_ = ga_func.ST_Contains(
            from_shape(ra, use_srid),
            getattr(cls, geom_attr)
        )
        filter_ = filter2_ if filter1_ is None else and_(filter1_, filter2_)

        return proto.read(self.request, filter=filter_)
 def update_distrib_point():
     """
     Update or create a new distribution point.
     For a given set of coordinates, it finds the Mantika (polygon)
     countaining the point and the closest Baladiya (point) and Muhalla (point)
     within the Mantika
     """
     id = request.form.get("id")
     distributionpoint = request.form.get("name_detailed")
     lon = request.form.get("longitude")
     lat = request.form.get("latitude")
     point = 'SRID=4326;POINT(' + lon + ' ' + lat + ')'
     mantika_q = db.session.query(
         libya_admin2.adm2_manti, libya_admin2.geom).filter(
             func.ST_Contains(libya_admin2.geom, point)).first()
     if mantika_q is None:
         wrong_coords = "Error: Coordinates " + lon + ", " + lat + " are not in Libya"
         return redirect(url_for('error', message=wrong_coords))
     mantika = mantika_q.adm2_manti
     baladiya = db.session.query(libya_admin3.adm3_balad) \
         .filter(func.ST_within(libya_admin3.geom, mantika_q.geom)) \
         .order_by(Comparator.distance_centroid(libya_admin3.geom, point)).limit(1).first()[0],  # Filters Baladiya (Points) in Mantika (Polygon) and finds the closest one to submited Point (KNN search with tree index)
     muhalla = db.session.query(libya_admin4.adm4_muhal) \
                 .filter(func.ST_within(libya_admin4.geom, mantika_q.geom)) \
                 .order_by(Comparator.distance_centroid(libya_admin4.geom, point)).limit(1).first()[0]  # Filters Muhalla (Points) in Mantika (Polygon) and finds the closest one to submited Point (KNN search with tree index)
     distri_point = distribution_points2.query.filter_by(id=id).first()
     if distri_point is None:
         new_distri_point = distribution_points2(
             distributionpoint=distributionpoint,
             partner=request.form.get("selectcp"),
             activity=request.form.get("selectactivity"),
             idps=request.form.get("idps") in ["True"],
             returnees=request.form.get("returnees") in ["True"],
             nondisplaced=request.form.get("nondisplaced") in ["True"],
             migrants=request.form.get("migrants") in ["True"],
             comments=request.form.get("comments"),
             geom=point,
             mantika=mantika,
             baladiya=baladiya,
             muhalla=muhalla)
         db.session.add(new_distri_point)
     else:
         distri_point.distributionpoint = distributionpoint
         distri_point.geom = point
         distri_point.mantika = mantika
         distri_point.baladiya = baladiya
         distri_point.muhalla = muhalla
     db.session.commit()
     return redirect(url_for('home'))
Example #4
0
    def _proto_read(self, layer: "main.Layer") -> FeatureCollection:
        """ Read features for the layer based on the self.request. """
        from c2cgeoportal_commons.models.main import (  # pylint: disable=import-outside-toplevel
            Layer, RestrictionArea, Role,
        )

        proto = self._get_protocol_for_layer(layer)
        if layer.public:
            return proto.read(self.request)
        user = self.request.user
        if user is None:
            raise HTTPForbidden()
        cls = proto.mapped_class
        geom_attr = proto.geom_attr
        ras = models.DBSession.query(RestrictionArea.area,
                                     RestrictionArea.area.ST_SRID())
        ras = ras.join(RestrictionArea.roles)
        ras = ras.join(RestrictionArea.layers)
        ras = ras.filter(Role.id.in_(get_roles_id(self.request)))
        ras = ras.filter(Layer.id == layer.id)
        collect_ra = []
        use_srid = -1
        for ra, srid in ras.all():
            if ra is None:
                return proto.read(self.request)
            use_srid = srid
            collect_ra.append(to_shape(ra))
        if not collect_ra:
            raise HTTPForbidden()

        filter1_ = create_filter(self.request, cls, geom_attr)
        ra = cascaded_union(collect_ra)
        filter2_ = ga_func.ST_Contains(from_shape(ra, use_srid),
                                       getattr(cls, geom_attr))
        filter_ = filter2_ if filter1_ is None else and_(filter1_, filter2_)

        feature = proto.read(self.request, filter=filter_)
        if isinstance(feature, HTTPException):
            raise feature  # pylint: disable=raising-non-exception
        return feature
Example #5
0
    def index(self):
        if request.args.get('bounds') is None:
            return Response(json.dumps([]), mimetype='application/json')

        bounds_parts = request.args.get("bounds").split(',')
        points = Point.query.filter(
            func.ST_Contains(
                func.ST_MakeEnvelope(
                    bounds_parts[1],
                    bounds_parts[0],
                    bounds_parts[3],
                    bounds_parts[2]),
                Point.geom
            )
        ).filter(
            Point.deleted_by_user.isnot(True)
        ).filter(
            or_(Point.approved, Point.revised == False)
        ).all()
        points = list(map(lambda point: point.serialize(), points))

        return Response(json.dumps(points),  mimetype='application/json')
Example #6
0
    def points_of_crowdsourcing_day(self):
        if request.args.get('bounds') is None:
            return Response(json.dumps([]), mimetype='application/json')

        bounds_parts = request.args.get("bounds").split(',')
        points = Point.query.filter(
            func.ST_Contains(
                func.ST_MakeEnvelope(
                    bounds_parts[1],
                    bounds_parts[0],
                    bounds_parts[3],
                    bounds_parts[2]),
                Point.geom
            )
        ).filter(
            Point.deleted_by_user.isnot(True)
        ).filter(
            or_(Point.approved, Point.revised == False)
        ).filter(
            or_(func.substr(Point.properties[('tags', 'timestamp')].astext, 1, 10) == "2017-05-09")
        ).all()
        points = list(map(lambda point: point.serialize(), points))

        return Response(json.dumps(points),  mimetype='application/json')
    def index(self):
        polygons = db.session.query(
            CrowdsourcingPolygon.id, CrowdsourcingPolygon.geom,
            func.count(Point.geom).label("num_points")).outerjoin(
                Point,
                and_(
                    func.ST_Contains(CrowdsourcingPolygon.geom, Point.geom),
                    func.substr(Point.properties[('tags', 'timestamp')].astext,
                                1, 10) == "2017-05-09")).group_by(
                                    CrowdsourcingPolygon.id)

        #map(self.mostPoints, polygons)
        for polygon in polygons:
            self.mostPoints(polygon)

        polygons = map(
            lambda polygon: {
                'id': polygon.id,
                'geom': list(to_shape(polygon.geom).exterior.coords),
                'num_points': polygon.num_points,
                'is_winner': self.is_winner(polygon.id)
            }, polygons)
        polygons = list(polygons)
        return Response(json.dumps(polygons), mimetype='application/json')