Пример #1
0
 def nearest(self, point, max_radius_km=settings.MAP_SEARCH_RADIUS_KM):
     """Filter Erps around a given point, which can be either a `Point` instance
     or a tuple(lat, lon)."""
     qs = self.annotate(distance=Distance("geom", point))
     if max_radius_km:
         qs = qs.filter(distance__lt=measure.Distance(km=max_radius_km))
     return qs.order_by("distance")
Пример #2
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        params = self.request.GET.copy()
        now = datetime.now()
        params.setdefault('hour', now.hour)
        params.setdefault('weekday', now.weekday())
        params.setdefault('lng', -73.989774)
        params.setdefault('lat', 40.752336)

        hour = params.get('hour')
        weekday = params.get('weekday')
        lat = float(params.get('lat'))
        lng = float(params.get('lng'))

        point = Point(lng, lat, srid=4326)
        recommendations = (Recommendation.objects.filter(
            hour=hour, weekday=weekday).annotate(
                distance=Distance('poly', point)).annotate(point=Func(
                    F('poly'),
                    Func(Value(str(point)), function='ST_PointFromText'),
                    function='ST_ClosestPoint')).filter(
                        distance__lte=measure.Distance(
                            mi=5).m)).order_by('-score')[:10]
        context['recommendations'] = [list(r.point) for r in recommendations]

        context['form'] = RecommendationForm(data=params)
        return context
Пример #3
0
def find_by_distance(search_point, distance_threshold_km, limit, qs):
    queryset_spatial = qs.filter(geom__dwithin=(search_point, measure.Distance(km=distance_threshold_km))).annotate(distance=Distance("geom", search_point)).order_by("distance")

    if limit is not None:
        return queryset_spatial[:limit]
    else:
        return queryset_spatial
Пример #4
0
def test_fes20_c7_example1():
    """C.7 Temporal filter example

    EXAMPLE 1
    The following examples for temporal comparisons are provided to illustrate
    the proper use of the temporal  The examples are based on the
    following sample GML:"""
    xml_text = """
        <?xml version="1.0"?>
        <fes:Filter
           xmlns:fes="http://www.opengis.net/fes/2.0"
           xmlns:gml="http://www.opengis.net/gml/3.2"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.opengis.net/fes/2.0
           http://schemas.opengis.net/filter/2.0/filterAll.xsd
           http://www.opengis.net/gml/3.2
           http://schemas.opengis.net/gml/3.2.1/gml.xsd">
           <fes:DWithin>
              <fes:ValueReference>geometry</fes:ValueReference>
              <gml:Point gml:id="P1" srsName="http://www.opengis.net/def/crs/epsg/0/4326">
                 <gml:pos>43.716589 -79.340686</gml:pos>
              </gml:Point>
              <fes:Distance uom="m">10</fes:Distance>
           </fes:DWithin>
        </fes:Filter>
    """.strip()
    expected = Filter(predicate=DistanceOperator(
        valueReference=ValueReference(xpath="geometry"),
        operatorType=DistanceOperatorName.DWithin,
        geometry=geometries.GEOSGMLGeometry(
            srs=WGS84,
            geos_data=GEOSGeometry("POINT (43.716589 -79.34068600000001)",
                                   srid=4326),
        ),
        distance=Measure(value=10, uom="m"),
    ))
    result = Filter.from_string(xml_text)
    assert result == expected, f"result={result!r}"

    # Test SQL generating
    query = result.compile_query()
    assert query == CompiledQuery(lookups=[
        Q(geometry__dwithin=(
            GEOSGeometry("POINT (43.716589 -79.34068600000001)", srid=4326),
            measure.Distance(m=10),
        ))
    ]), repr(query)
Пример #5
0
def test_fes20_c5_example15():
    """This example finds features within a specified distance of a geometry."""
    xml_text = """
        <?xml version="1.0"?>
        <fes:Filter
           xmlns:fes="http://www.opengis.net/fes/2.0"
           xmlns:gml="http://www.opengis.net/gml/3.2"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.opengis.net/fes/2.0
           http://schemas.opengis.net/filter/2.0/filterAll.xsd
           http://www.opengis.net/gml/3.2
           http://schemas.opengis.net/gml/3.2.1/gml.xsd">
           <fes:DWithin>
              <fes:ValueReference>geometry</fes:ValueReference>
              <gml:Point gml:id="P1" srsName="http://www.opengis.net/def/crs/epsg/0/4326">
                 <gml:pos>43.716589 -79.340686</gml:pos>
              </gml:Point>
              <fes:Distance uom="m">10</fes:Distance>
           </fes:DWithin>
        </fes:Filter>
    """.strip()
    expected = Filter(predicate=DistanceOperator(
        valueReference=ValueReference(xpath="geometry"),
        operatorType=DistanceOperatorName.DWithin,
        geometry=geometries.GEOSGMLGeometry(
            srs=WGS84,
            geos_data=GEOSGeometry("POINT (43.716589 -79.34068600000001)",
                                   srid=4326),
        ),
        distance=Measure(value=D("10"), uom="m"),
    ))
    result = Filter.from_string(xml_text)
    assert result == expected, f"result={result!r}"

    # Test SQL generating
    query = result.compile_query()
    assert query == CompiledQuery(lookups=[
        Q(geometry__dwithin=(
            GEOSGeometry("POINT (43.716589 -79.34068600000001)", srid=4326),
            measure.Distance(m=10.0),
        ))
    ]), repr(query)
Пример #6
0
 def build_rhs(self, compiler) -> measure.Distance:
     return measure.Distance(default_unit=self.uom, **{self.uom: self.value})
Пример #7
0
 def apply(self, queryset):
     return queryset.filter(
         Q(location__isnull=True) | Q(distance__lte=(measure.Distance(km=self.radius))),
         initial_fee__gte=self.min_price,
         category__in=self.categories.all(),
     )
Пример #8
0
 def find_existing_matches(self, nom, geom):
     return self.nearest(geom).filter(
         nom__unaccent__lower__trigram_similar=nom,
         distance__lt=measure.Distance(m=200),
     )
Пример #9
0
Файл: models.py Проект: g10f/sso
 def transregional_measure_distance(self):
     if self.transregional_distance:
         return measure.Distance(**{'km': self.transregional_distance})
     else:
         return None
Пример #10
0
Файл: models.py Проект: g10f/sso
 def neighbour_measure_distance(self):
     if self.neighbour_distance:
         return measure.Distance(**{'km': self.neighbour_distance})
     else:
         return None