Пример #1
0
    def get_demands(self, route, direction_route, radius):
        """Return demands trip announces.
        
        Indifferent trips are fetched too.
        
        Entirely based on spatial criterias.

        Search for roads close from departure or arrival points. (close is 
        defined by the radius of the offer + the radius of the demand)
        
        Return offers witch have a percentage of matching route greater than
        MAX_PERCENTAGE_RANK.
        
        Calculations arent made from the simplified projected route

        """
        ogr = smart_transform(route, SRID_TRANSFORM,
                              from_srid=SRID_DEFAULT).ogr
        demands = self.filter(demand__isnull=False)
        demands = demands.select_related('user', 'demand')
        return demands.extra(select=SortedDict([
            ('pourcentage_rank', """get_pourcentage_rank(
                    ST_GeomFromText(%s, %s),
                    "carpool_trip"."departure_point",
                    "carpool_trip"."arrival_point"
                )
                """),
        ]),
                             where=[
                                 """ST_DWithin(
                    ST_GeomFromText(%s, %s),
                    "carpool_trip"."departure_point_proj",
                    "carpool_tripdemand"."radius" + %s
                )
                """,
                                 """ST_DWithin(
                    ST_GeomFromText(%s, %s),
                    "carpool_trip"."arrival_point_proj",
                    "carpool_tripdemand"."radius" + %s
                )
                """,
                                 """get_pourcentage_rank(
                    ST_GeomFromText(%s, %s),
                    "carpool_trip"."departure_point",
                    "carpool_trip"."arrival_point"
                ) >= %s
                """,
                             ],
                             params=[
                                 ogr.wkt,
                                 SRID_TRANSFORM,
                                 radius,
                                 ogr.wkt,
                                 SRID_TRANSFORM,
                                 radius,
                                 direction_route.wkt,
                                 SRID_DEFAULT,
                                 self.MAX_PERCENTAGE_RANK,
                             ],
                             select_params=(direction_route.wkt, SRID_DEFAULT))
Пример #2
0
    def get_demands(self, route, direction_route, radius):
        """Return demands trip announces.
        
        Indifferent trips are fetched too.
        
        Entirely based on spatial criterias.

        Search for roads close from departure or arrival points. (close is 
        defined by the radius of the offer + the radius of the demand)
        
        Return offers witch have a percentage of matching route greater than
        MAX_PERCENTAGE_RANK.
        
        Calculations arent made from the simplified projected route

        """
        ogr = smart_transform(route, SRID_TRANSFORM, from_srid=SRID_DEFAULT).ogr
        demands = self.filter(demand__isnull=False)
        demands = demands.select_related('user', 'demand')
        print "IN DEMANDS #################"
        return demands.extra(
            select=SortedDict([
                ('pourcentage_rank', """get_pourcentage_rank(
                    ST_GeomFromText(%s, %s),
                    "carpool_trip"."departure_point",
                    "carpool_trip"."arrival_point"
                )
                """),
            ]),
            where=[
                """ST_DWithin(
                    ST_GeomFromText(%s, %s),
                    "carpool_trip"."departure_point_proj",
                    "carpool_tripdemand"."radius" + %s
                )
                """,
                """ST_DWithin(
                    ST_GeomFromText(%s, %s),
                    "carpool_trip"."arrival_point_proj",
                    "carpool_tripdemand"."radius" + %s
                )
                """,
                """get_pourcentage_rank(
                    ST_GeomFromText(%s, %s),
                    "carpool_trip"."departure_point",
                    "carpool_trip"."arrival_point"
                ) >= %s
                """,
            ],
            params=[
                ogr.wkt, SRID_TRANSFORM,
                radius,
                ogr.wkt, SRID_TRANSFORM,
                radius,
                direction_route.wkt, SRID_DEFAULT,
                self.MAX_PERCENTAGE_RANK,
            ],
            select_params=(direction_route.wkt, SRID_DEFAULT)
        )
Пример #3
0
def get_simple_route(route):
    """Retourne une version simplifiée d'une route."""
    num_points = route.num_points
    if num_points <= MAX_NUM_POINTS:
        return route
    tolerance = TOLERANCE
    if num_points <= MAX_NUM_POINTS * 3:
        tolerance = MIN_TOLERANCE
    route = smart_transform(route, SRID_TRANSFORM, from_srid=SRID_DEFAULT)
    simple_route = route.simplify(tolerance)
    ogr = simple_route.ogr
    ogr.transform(SRID_DEFAULT)
    return GEOSGeometry(ogr.wkb, SRID_DEFAULT)
Пример #4
0
def get_simple_route(route):
    """Retourne une version simplifiée d'une route."""
    num_points = route.num_points
    if num_points <= MAX_NUM_POINTS:
        return route
    tolerance = TOLERANCE
    if num_points <= MAX_NUM_POINTS * 3:
        tolerance = MIN_TOLERANCE
    route = smart_transform(route, SRID_TRANSFORM, from_srid=SRID_DEFAULT)
    simple_route = route.simplify(tolerance)
    ogr = simple_route.ogr
    ogr.transform(SRID_DEFAULT)
    return GEOSGeometry(ogr.wkb, SRID_DEFAULT)
Пример #5
0
def get_buffer_from_geometry(geometry, radius):
    """Get the buffer of the geometry around a radius."""
    if isinstance(geometry, LineString):
        mgeom = MultiLineString([geometry])
    else:
        mgeom = geometry
    polys = None
    for geom in mgeom:
        geom = smart_transform(geom, SRID_TRANSFORM, from_srid=SRID_DEFAULT)
        poly_buffer = geom.buffer(radius)
        if not poly_buffer.empty:
            polys = polys.union(poly_buffer) if polys else poly_buffer
    ogr = polys.ogr
    ogr.transform(SRID_DEFAULT)
    return ogr
Пример #6
0
def get_buffer_from_geometry(geometry, radius):
    """Get the buffer of the geometry around a radius."""
    if isinstance(geometry, LineString):
        mgeom = MultiLineString([geometry])
    else:
        mgeom = geometry
    polys = None
    for geom in mgeom:
        geom = smart_transform(geom, SRID_TRANSFORM, from_srid=SRID_DEFAULT)
        poly_buffer = geom.buffer(radius)
        if not poly_buffer.empty:
            polys = polys.union(poly_buffer) if polys else poly_buffer
    ogr = polys.ogr
    ogr.transform(SRID_DEFAULT)
    return ogr
Пример #7
0
    def get_trip_to_city(self, arrival_point, radius):
        """Return announces bounds to a city, with a radius parameter
        
        Don't use route, results are just based from the arrival city

        """
        ogr = smart_transform(arrival_point,SRID_TRANSFORM, from_srid=SRID_DEFAULT).ogr
        return Trip.objects.select_related().extra(
            select = {
                'type': ('CASE WHEN demand_id IS NULL THEN 0 '
                    'WHEN offer_id IS NULL THEN 1 ELSE 2 END')
            },
            where = [
                'ST_DWithin(ST_PointFromWKB(%s, %s), '
                '"carpool_trip"."arrival_point_proj", %s)',
            ],
            params = [
                ogr.wkb, SRID_TRANSFORM,
                radius,
            ]
        )
Пример #8
0
    def get_trip_from_city(self, departure_point, radius):
        """Return announces originally a city with a radius parameter
        
        Don't use the route, results are just based from the departure city

        """
        ogr = smart_transform(departure_point,SRID_TRANSFORM, from_srid=SRID_DEFAULT).ogr
        return Trip.objects.select_related().extra(
            select = {
                'type': ('CASE WHEN demand_id IS NULL THEN 0 '
                    'WHEN offer_id IS NULL THEN 1 ELSE 2 END'),
            },
            where = [
                ('ST_DWithin(ST_PointFromWKB(%s, %s), '
                    '"carpool_trip"."departure_point_proj", %s)'),
            ],
            params = [
                ogr.wkb, SRID_TRANSFORM,
                radius,
            ],
        )
Пример #9
0
    def get_trip_to_city(self, arrival_point, radius):
        """Return announces bounds to a city, with a radius parameter
        
        Don't use route, results are just based from the arrival city

        """
        ogr = smart_transform(arrival_point,
                              SRID_TRANSFORM,
                              from_srid=SRID_DEFAULT).ogr
        return Trip.objects.select_related().extra(
            select={
                'type': ('CASE WHEN demand_id IS NULL THEN 0 '
                         'WHEN offer_id IS NULL THEN 1 ELSE 2 END')
            },
            where=[
                'ST_DWithin(ST_PointFromWKB(%s, %s), '
                '"carpool_trip"."arrival_point_proj", %s)',
            ],
            params=[
                ogr.wkb,
                SRID_TRANSFORM,
                radius,
            ])
Пример #10
0
    def get_trip_from_city(self, departure_point, radius):
        """Return announces originally a city with a radius parameter
        
        Don't use the route, results are just based from the departure city

        """
        ogr = smart_transform(departure_point,
                              SRID_TRANSFORM,
                              from_srid=SRID_DEFAULT).ogr
        return Trip.objects.select_related().extra(
            select={
                'type': ('CASE WHEN demand_id IS NULL THEN 0 '
                         'WHEN offer_id IS NULL THEN 1 ELSE 2 END'),
            },
            where=[
                ('ST_DWithin(ST_PointFromWKB(%s, %s), '
                 '"carpool_trip"."departure_point_proj", %s)'),
            ],
            params=[
                ogr.wkb,
                SRID_TRANSFORM,
                radius,
            ],
        )
Пример #11
0
    def get_offers(self, departure_point, arrival_point, radius):
        """Fetch and return offer trip announces.
        
        Indifferent trips are fetched too.
        
        Entierely based on spatial criterias
        
        Search for roads close from departure or arrival points. (close is 
        defined by the radius of the offer + the radius of the demand)
        
        Return offers witch have a percentage of matching route greater than
        MAX_PERCENTAGE_RANK.
        
        Calculations are made from the simplified projected route

        """
        # rem: simple_route_proj and direction_route_proj are not modelized,
        # but are filled by a trigger
        ogr_departure = smart_transform(departure_point,
                                        SRID_TRANSFORM,
                                        from_srid=SRID_DEFAULT).ogr
        ogr_arrival = smart_transform(arrival_point,
                                      SRID_TRANSFORM,
                                      from_srid=SRID_DEFAULT).ogr
        offers = self.filter(offer__isnull=False)
        offers = offers.select_related('user', 'offer')
        return offers.extra(select=SortedDict([
            ('pourcentage_rank', """get_pourcentage_rank(
                    "carpool_tripoffer"."direction_route_proj",
                    ST_GeomFromText(%s, %s),
                    ST_GeomFromText(%s, %s)
                )
                """),
        ]),
                            select_params=(ogr_departure.wkt, SRID_TRANSFORM,
                                           ogr_arrival.wkt, SRID_TRANSFORM),
                            where=[
                                """ST_DWithin(
                    "carpool_tripoffer"."simple_route_proj",
                    ST_GeomFromText(%s, %s),
                    "carpool_tripoffer"."radius" + %s
                )
                """,
                                """ST_DWithin(
                    "carpool_tripoffer"."simple_route_proj",
                    ST_GeomFromText(%s, %s),
                    "carpool_tripoffer"."radius" + %s
                )
                """,
                                """get_pourcentage_rank(
                    "carpool_tripoffer"."direction_route_proj",
                    ST_GeomFromText(%s, %s),
                    ST_GeomFromText(%s, %s)
                ) >= %s
                """,
                            ],
                            params=[
                                ogr_departure.wkt,
                                SRID_TRANSFORM,
                                radius,
                                ogr_arrival.wkt,
                                SRID_TRANSFORM,
                                radius,
                                ogr_departure.wkt,
                                SRID_TRANSFORM,
                                ogr_arrival.wkt,
                                SRID_TRANSFORM,
                                self.MAX_PERCENTAGE_RANK,
                            ])
Пример #12
0
    def get_offers(self, departure_point, arrival_point, radius):
        """Fetch and return offer trip announces.
        
        Indifferent trips are fetched too.
        
        Entierely based on spatial criterias
        
        Search for roads close from departure or arrival points. (close is 
        defined by the radius of the offer + the radius of the demand)
        
        Return offers witch have a percentage of matching route greater than
        MAX_PERCENTAGE_RANK.
        
        Calculations are made from the simplified projected route

        """
        # rem: simple_route_proj and direction_route_proj are not modelized,
        # but are filled by a trigger
        ogr_departure = smart_transform(departure_point,SRID_TRANSFORM, from_srid=SRID_DEFAULT).ogr
        ogr_arrival = smart_transform(arrival_point,SRID_TRANSFORM, from_srid=SRID_DEFAULT).ogr
        offers = self.filter(offer__isnull=False)
        offers = offers.select_related('user', 'offer')
        return offers.extra(
            select=SortedDict([
                ('pourcentage_rank', """get_pourcentage_rank(
                    "carpool_tripoffer"."direction_route_proj",
                    ST_GeomFromText(%s, %s),
                    ST_GeomFromText(%s, %s)
                )
                """),
            ]),
            select_params=(
                ogr_departure.wkt, SRID_TRANSFORM,
                ogr_arrival.wkt, SRID_TRANSFORM
            ),
            where=[
                """ST_DWithin(
                    "carpool_tripoffer"."simple_route_proj",
                    ST_GeomFromText(%s, %s),
                    "carpool_tripoffer"."radius" + %s
                )
                """,
                """ST_DWithin(
                    "carpool_tripoffer"."simple_route_proj",
                    ST_GeomFromText(%s, %s),
                    "carpool_tripoffer"."radius" + %s
                )
                """,
                """get_pourcentage_rank(
                    "carpool_tripoffer"."direction_route_proj",
                    ST_GeomFromText(%s, %s),
                    ST_GeomFromText(%s, %s)
                ) >= %s
                """,
            ],
            params=[
                ogr_departure.wkt, SRID_TRANSFORM, radius,
                ogr_arrival.wkt,SRID_TRANSFORM, radius,
                ogr_departure.wkt, SRID_TRANSFORM,
                ogr_arrival.wkt, SRID_TRANSFORM,
                self.MAX_PERCENTAGE_RANK,
            ]
        )