Пример #1
0
 def test_is_mgrs(self):
     valid_spaced = "18S TJ 90000 10000"
     valid_no_space = "18STJ9000010000"
     invalid = "180 TJ S0000 10000"
     self.assertTrue(is_mgrs(valid_spaced))
     self.assertTrue(is_mgrs(valid_no_space))
     self.assertFalse(is_mgrs(invalid))
Пример #2
0
 def test_is_mgrs(self):
     valid_spaced = '18S TJ 90000 10000'
     valid_no_space = '18STJ9000010000'
     invalid = '180 TJ S0000 10000'
     self.assertTrue(is_mgrs(valid_spaced))
     self.assertTrue(is_mgrs(valid_no_space))
     self.assertFalse(is_mgrs(invalid))
Пример #3
0
def search(request):
    """
    Detects the query type and calls the relevant geocoder to get results
    :param request: User request which should include a query parameter
    :return: A geojson with features matching the search query
    """
    q = request.GET.get('query', None)
    if not q:
        return HttpResponse(status=204, content_type="application/json")

    error_string = "An unknown error occurred while querying for results, please contact an administrator."
    degree_range = 0.05
    if is_mgrs(q):
        # check for necessary settings
        if getattr(settings, 'CONVERT_API_URL') is None:
            return HttpResponse('No Convert API specified', status=501)

        if getattr(settings, 'REVERSE_GEOCODING_API_URL') is None:
            return HttpResponse('No Reverse Geocode API specified', status=501)

        # make call to convert which should return a geojson feature of the MGRS location
        convert = CoordinateConverter()
        try:
            mgrs_data = convert.get(q)
        except Exception:
            return HttpResponse(content=error_string, status=500)

        # if no feature geom return nothing
        if not mgrs_data or not mgrs_data.get('geometry'):
            return HttpResponse(status=204, content_type="application/json")

        features = []
        # save the mgrs feature to return later
        if not mgrs_data.get('properties'):
            mgrs_data['properties'] = {}
        mgrs_data['properties']['bbox'] = [
            mgrs_data.get('geometry').get('coordinates')[0] - degree_range,
            mgrs_data.get('geometry').get('coordinates')[1] - degree_range,
            mgrs_data.get('geometry').get('coordinates')[0] + degree_range,
            mgrs_data.get('geometry').get('coordinates')[1] + degree_range
        ]
        mgrs_data['source'] = 'MGRS'
        features.append(mgrs_data)

        # call reverse to get a list of results near the mgrs feature
        reverse = ReverseGeocode()
        try:
            result = reverse.search({
                "point.lat":
                mgrs_data.get('geometry').get('coordinates')[1],
                "point.lon":
                mgrs_data.get('geometry').get('coordinates')[0]
            })
        except Exception:
            return HttpResponse(content=error_string, status=500)

        if result.get('features'):
            # add the mgrs feature with the search results and return together
            result['features'] = features + result['features']
            return HttpResponse(content=json.dumps(result),
                                status=200,
                                content_type="application/json")
        # if no results just return the MGRS feature in the response
        return HttpResponse(content=json.dumps({'features': features}),
                            status=200,
                            content_type="application/json")

    elif is_lat_lon(q):
        coords = is_lat_lon(q)
        # if no reverse url return 501
        if getattr(settings, 'REVERSE_GEOCODING_API_URL') is None:
            return HttpResponse('No Reverse Geocode API specified', status=501)

        # make call to reverse geocode
        reverse = ReverseGeocode()
        try:
            result = reverse.search({
                "point.lat": coords[0],
                "point.lon": coords[1]
            })
        except Exception:
            return HttpResponse(content=error_string, status=500)

        # create a feature representing the exact lat/lon being searched
        point_feature = {
            "geometry": {
                "type": "Point",
                "coordinates": [coords[1], coords[0]]
            },
            "source": "Coordinate",
            "type": "Feature",
            "properties": {
                "name":
                "{0} {1}, {2} {3}".format(
                    coords[0] if coords[0] >= 0 else coords[0] * -1,
                    "N" if coords[0] >= 0 else "S",
                    coords[1] if coords[1] >= 0 else coords[1] * -1,
                    "E" if coords[1] >= 0 else "W"),
                "bbox": [
                    coords[1] - degree_range, coords[0] - degree_range,
                    coords[1] + degree_range, coords[0] + degree_range
                ]
            }
        }
        # if there are results add the point feature and return them together
        if result.get('features'):
            result.get('features').insert(0, point_feature)
            return HttpResponse(content=json.dumps(result),
                                status=200,
                                content_type="application/json")
        # if there are no results return only the point feature
        features = {'features': [point_feature]}
        return HttpResponse(content=json.dumps(features),
                            status=200,
                            content_type="application/json")
    else:
        # make call to geocode with search
        geocode = Geocode()
        try:
            result = geocode.search(q)
        except Exception as e:
            logger.error(e)
            return HttpResponse(content=error_string, status=500)
        return HttpResponse(content=json.dumps(result),
                            status=200,
                            content_type="application/json")
Пример #4
0
def search(request):
    """
    Detects the query type and calls the relevant geocoder to get results
    :param request: User request which should include a query parameter
    :return: A geojson with features matching the search query
    """
    q = request.GET.get('query', None)
    if not q:
        return HttpResponse(status=204, content_type="application/json")

    error_string = "An unknown error occurred while querying for results, please contact an administrator."
    degree_range = 0.05
    if is_mgrs(q):
        # check for necessary settings
        if getattr(settings, 'CONVERT_API_URL') is None:
            return HttpResponse('No Convert API specified', status=501)

        if getattr(settings, 'REVERSE_GEOCODING_API_URL') is None:
            return HttpResponse('No Reverse Geocode API specified', status=501)

        # make call to convert which should return a geojson feature of the MGRS location
        convert = CoordinateConverter()
        try:
            mgrs_data = convert.get(q)
        except Exception:
            return HttpResponse(
                content=error_string,
                status=500
            )

        # if no feature geom return nothing
        if not mgrs_data or not mgrs_data.get('geometry'):
            return HttpResponse(status=204, content_type="application/json")

        features = []
        # save the mgrs feature to return later
        if not mgrs_data.get('properties'):
            mgrs_data['properties'] = {}
        mgrs_data['properties']['bbox'] = [
            mgrs_data.get('geometry').get('coordinates')[0] - degree_range,
            mgrs_data.get('geometry').get('coordinates')[1] - degree_range,
            mgrs_data.get('geometry').get('coordinates')[0] + degree_range,
            mgrs_data.get('geometry').get('coordinates')[1] + degree_range
        ]
        mgrs_data['source'] = 'MGRS'
        features.append(mgrs_data)

        # call reverse to get a list of results near the mgrs feature
        reverse = ReverseGeocode()
        try:
            result = reverse.search({
                "point.lat": mgrs_data.get('geometry').get('coordinates')[1],
                "point.lon": mgrs_data.get('geometry').get('coordinates')[0]
            })
        except Exception:
            return HttpResponse(
                content=error_string,
                status=500
            )

        if result.get('features'):
            # add the mgrs feature with the search results and return together
            result['features'] = features + result['features']
            return HttpResponse(content=json.dumps(result), status=200, content_type="application/json")
        # if no results just return the MGRS feature in the response
        return HttpResponse(content=json.dumps({'features': features}), status=200, content_type="application/json")

    elif is_lat_lon(q):
        coords = is_lat_lon(q)
        # if no reverse url return 501
        if getattr(settings, 'REVERSE_GEOCODING_API_URL') is None:
            return HttpResponse('No Reverse Geocode API specified', status=501)

        # make call to reverse geocode
        reverse = ReverseGeocode()
        try:
            result = reverse.search({
                "point.lat": coords[0],
                "point.lon": coords[1]
            })
        except Exception:
            return HttpResponse(
                content=error_string,
                status=500
            )

        # create a feature representing the exact lat/lon being searched
        point_feature = {
            "geometry": {
                "type": "Point",
                "coordinates": [coords[1], coords[0]]
            },
            "source": "Coordinate",
            "type": "Feature",
            "properties": {
                "name": "{0} {1}, {2} {3}".format(
                    coords[0] if coords[0] >= 0 else coords[0] * -1,
                    "N" if coords[0] >= 0 else "S",
                    coords[1] if coords[1] >= 0 else coords[1] * -1,
                    "E" if coords[1] >= 0 else "W"
                ),
                "bbox": [
                    coords[1] - degree_range,
                    coords[0] - degree_range,
                    coords[1] + degree_range,
                    coords[0] + degree_range
                ]
            }
        }
        # if there are results add the point feature and return them together
        if result.get('features'):
            result.get('features').insert(0, point_feature)
            return HttpResponse(content=json.dumps(result), status=200, content_type="application/json")
        # if there are no results return only the point feature
        features = {'features': [point_feature]}
        return HttpResponse(content=json.dumps(features), status=200, content_type="application/json")
    else:
        # make call to geocode with search
        geocode = Geocode()
        try:
            result = geocode.search(q)
        except Exception as e:
            logger.error(e)
            return HttpResponse(
                content=error_string,
                status=500
            )
        return HttpResponse(content=json.dumps(result), status=200, content_type="application/json")