Exemplo n.º 1
0
def get_lake_from_pt(request):
    """This function accepts post requests that contain a geojson
    representation of a point.  The view returns a dictionary contianing
    the id, abbreviation, name, centroid and bounds (extent) of the lake
    containing the point, or an empty dictionary if the dat is not geojson
    or falls outside of any lake.

    TODO: add options for 'pure' and 'plus' geometries

    """

    geom = request.query_params.get("geom")
    pt = parse_geom(request.data.get("point"))
    if pt is None:
        return Response({}, status=status.HTTP_400_BAD_REQUEST)

    lake = Lake.objects.filter(geom__contains=pt).first()

    if lake:
        ret = dict(
            id=lake.id,
            abbrev=lake.abbrev,
            lake_name=lake.lake_name,
            centroid=lake.geom.centroid.wkt,
            extent=lake.geom.extent,
        )

        if geom == "geom":
            ret["geom"] = lake.geom.geojson

        return Response(ret, status=status.HTTP_200_OK)
    else:
        # no lake object could be associated with that point.
        # 400
        return Response({}, status=status.HTTP_404_NOT_FOUND)
Exemplo n.º 2
0
def pt_spatial_attrs(request):
    """This function accepts post requests that contain a geojson
    representation of a point and returns a dictionary containing the
    basic elements needed to validate the spatial widgets in the
    stocking event and stocking upload forms.  Given a lat-lon, return
    a dictionary with the following elements:

    + lake - with id, lake name, lake abbrev
    + juristiction - with id, stateprov name, stateprov abbrev
    + manUnit - id, label
    + grid10 - id, label, lake abbrev.

    post data should contain a json string of the form:

    {"point": "POINT(-81.5 44.5)"}


    """

    pt = parse_geom(request.data.get("point"))
    if pt is None:
        return Response({}, status=status.HTTP_400_BAD_REQUEST)

    polygons = get_polygons(pt)

    return Response(polygons, status=status.HTTP_200_OK)
Exemplo n.º 3
0
def get_grid10_from_pt(request):
    """This function accepts post requests that contain a geojson
    representation of a point.  The view returns a dictionary contianing
    the id, abbreviation, name, centroid and bounds (extent) of the grid10
    containing the point, or an empty dictionary if the dat is not geojson
    or falls outside of any grid10.

    TODO: Add polygon field for grid10 objects so that we can find out
    which grid the point falls in. This function finds the closest
    centroid which is slower and likely to have lots of failing edge
    cases.

    post request should be of the form:

    {"point": "POINT(-81.5 44.5)"}

    """

    pt = parse_geom(request.data.get("point"))
    if pt is None:
        return Response({}, status=status.HTTP_400_BAD_REQUEST)

    grid10 = Grid10.objects.select_related("lake").filter(
        geom__contains=pt).first()

    geom = request.query_params.get("geom")

    if grid10:
        ret = dict(
            id=grid10.id,
            grid=grid10.grid,
            slug=grid10.slug,
            centroid=grid10.geom.centroid.wkt,
            extent=grid10.geom.extent,
            # lake attributes:
            lake_id=grid10.lake.id,
            lake_abbrev=grid10.lake.abbrev,
            lake_name=grid10.lake.lake_name,
        )
        if geom == "geom":
            ret["geom"] = grid10.geom.geojson

        return Response(ret, status=status.HTTP_200_OK)
    else:
        # no grid10 object could be associated with that point.
        # 400
        return Response({}, status=status.HTTP_404_NOT_FOUND)
Exemplo n.º 4
0
def get_management_unit_from_pt(request):
    """This function accepts post requests that contains a geojson
    representation of a point.  The view returns a dictionary contianing
    the id, label, mu_type, centroid and bounds (extent) of the management_unit
    containing the point, or an empty dictionary if the data is not geojson
    or falls outside of any management_unit.

    This function takes an additional argument (mu_type) as a query
    parameter that controls what type of managemnet unit is returned -
    current options are stat_dist, mu, qma, ltrz.  Others could be
    added in the future.  If the mu_type argument is not included in
    the request, the management_unit with primary=True is returned by
    default.

    TODO: add options for 'pure' and 'plus' geometries

    """
    geom = request.query_params.get("geom")
    mu_type = request.query_params.get("mu_type")
    all_mus = request.query_params.get("all")

    pt = parse_geom(request.data.get("point"))
    if pt is None:
        return Response({}, status=status.HTTP_400_BAD_REQUEST)

    qs = ManagementUnit.objects.filter(geom__contains=pt)

    if all_mus and all_mus in ["T", "t", "TRUE", "True", "true"]:
        qs = qs.all()
    elif mu_type:
        qs = qs.filter(mu_type=mu_type).first()
    else:
        qs = qs.filter(primary=True).first()

    if qs:
        if all_mus:
            ret = [manUnit_dict(x, geom) for x in qs]
        else:
            ret = manUnit_dict(qs, geom)

        return Response(ret, status=status.HTTP_200_OK)
    else:
        # no qs object could be associated with that point.
        # 400
        return Response({}, status=status.HTTP_404_NOT_FOUND)
Exemplo n.º 5
0
def get_jurisdiction_from_pt(request):
    """This function accepts post requests that contain a geojson
    representation of a point.  The view returns a dictionary contianing
    the id, abbreviation, name, centroid and bounds (extent) of the jurisdiction
    containing the point, or an empty dictionary if the dat is not geojson
    or falls outside of any jurisdiction.

    TODO: add options for 'pure' and 'plus' geometries

    """

    geom = request.query_params.get("geom")
    pt = parse_geom(request.data.get("point"))
    if pt is None:
        return Response({}, status=status.HTTP_400_BAD_REQUEST)

    jurisdiction = (Jurisdiction.objects.select_related(
        "lake", "stateprov").filter(geom__contains=pt).first())

    if jurisdiction:
        ret = dict(
            id=jurisdiction.id,
            # lake attributes:
            lake_id=jurisdiction.lake.id,
            lake_abbrev=jurisdiction.lake.abbrev,
            lake_name=jurisdiction.lake.lake_name,
            # state prov. attributes:
            stateprov_id=jurisdiction.stateprov.id,
            stateprov_abbrev=jurisdiction.stateprov.abbrev,
            stateprov_name=jurisdiction.stateprov.name,
            # jurisdiction attributes
            jurisdiction_name=jurisdiction.name,
            centroid=jurisdiction.geom.centroid.wkt,
            extent=jurisdiction.geom.extent,
        )

        if geom == "geom":
            ret["geom"] = jurisdiction.geom.geojson

        return Response(ret, status=status.HTTP_200_OK)
    else:
        # no jurisdiction object could be associated with that point.
        # 400
        return Response({}, status=status.HTTP_404_NOT_FOUND)
Exemplo n.º 6
0
def roi_spatial_attrs(request):
    """This function accepts post requests that contain a geojson
    representation of a polygon and returns a dictionary containing
    the basic elements needed to validate the find-events forms and
    limit the choices in the drop-downs for spatial fields to the
    enties that overlap with the roi.  (if the region of interest in
    entirely within lake michigan - there is no point in including the
    aother lakes, or their children in the select widget).

    Given a valid polygon geometry, return a dictionary with the following elements:

    + lakes - list of lakes with id, lake name, lake abbrev

    + juristictions - list of jurisdictions with id, stateprov name, stateprov abbrev

    + manUnits - list of management units including the slug,
      management unit type, and wether or not it should be considered to
      be a primary management unit.

    post data should contain a json string of the form:

    {"roi":
    "POLYGON ((-80.98 45.03, -81.15 44.90, -81.02 44.42, -80.38 44.45, -80.33 44.85, -80.98 45.03))"
    }

    """

    roi = parse_geom(request.data.get("roi"))

    if roi is None:
        return Response({}, status=status.HTTP_400_BAD_REQUEST)

    ret = dict()

    lakes = Lake.objects.filter(geom__intersects=roi).values(
        "id", "abbrev", "lake_name")

    if lakes:
        ret["lakes"] = list(lakes)
    else:
        ret["lakes"] = ""

    jurisdictions = (
        Jurisdiction.objects.select_related(
            "lake", "stateprov").filter(geom__intersects=roi).values(
                # jurisdiction attributes
                "id",
                "lake_id",
                "stateprov_id",
                jurisd=F("slug"),
                jurisdiction_name=F("name"),
                # centroid=F("geom__centroid__wkt"),
                # lake attributes:
                lake_abbrev=F("lake__abbrev"),
                lake_name=F("lake__lake_name"),
                # state prov__ attributes:
                stateprov_abbrev=F("stateprov__abbrev"),
                stateprov_name=F("stateprov__name"),
            ))

    if jurisdictions:
        ret["jurisdictions"] = list(jurisdictions)

    else:
        ret["jurisdictions"] = ""

    # get our stat disctricts too. for now just return the slugs
    man_units = (ManagementUnit.objects.filter(geom__intersects=roi).filter(
        primary=True).values("slug", "mu_type", "primary"))

    if man_units:
        ret["manUnits"] = list(man_units)
    else:
        ret["manUnits"] = ""

    return Response(ret, status=status.HTTP_200_OK)