Пример #1
0
    def test_convert_success(self):
        convert_response_success = {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [-112.61869345019069, 50.00105275281522]
            },
            "properties": {
                "name": "12UUA8440",
                "from": "mgrs",
                "to": "decdeg"
            },
        }

        self.mock_requests.get(mockURL,
                               text=json.dumps(convert_response_success),
                               status_code=200)
        convert = CoordinateConverter()
        result = convert.get("18S TJ 97100 03003")
        self.assertIsNotNone(result.get("geometry"))
        self.assertEqual(result.get("type"), "Feature")
        properties = result.get("properties")
        geometry = result.get("geometry")
        self.assertIsInstance(properties, dict)
        self.assertIsInstance(geometry, dict)
        self.assertEqual(geometry.get("type"), "Point")
        self.assertIsInstance(geometry.get("coordinates"), list)
 def convert_test_fail(self, api_response):
     self.mock_requests.get(mockURL, text=json.dumps(api_response), status_code=200)
     convert = CoordinateConverter()
     result = convert.get("12UUA844")
     self.assertIsNone(result.get("geometry"))
     properties = result.get("properties")
     self.assertIsInstance(properties, dict)
Пример #3
0
 def convert_test_fail(self, api_response):
     self.mock_requests.get(mockURL,
                            text=json.dumps(api_response),
                            status_code=200)
     convert = CoordinateConverter()
     result = convert.get("12UUA844")
     self.assertIsNone(result.get("geometry"))
     properties = result.get("properties")
     self.assertIsInstance(properties, dict)
Пример #4
0
def convert(request):
    convert = CoordinateConverter()
    if getattr(settings, 'CONVERT_API_URL') is not None:
        if request.GET.get('convert'):
            result = convert.get(request.GET.get('convert'))
            return HttpResponse(content=json.dumps(result), status=200, content_type="application/json")
        else:
            return HttpResponse(status=204, content_type="application/json")
    else:
        return HttpResponse('No Convert API specified', status=501)
Пример #5
0
def convert(request):
    convert = CoordinateConverter()
    if getattr(settings, 'CONVERT_API_URL') is not None:
        if request.GET.get('convert'):
            result = convert.get(request.GET.get('convert'))
            return HttpResponse(content=json.dumps(result), status=200, content_type="application/json")
        else:
            return HttpResponse(status=204, content_type="application/json")
    else:
        return HttpResponse('No Convert API specified', status=501)
 def convert_test_success(self, api_response):
     
     self.mock_requests.get(mockURL, text=json.dumps(api_response), status_code=200)
     convert = CoordinateConverter()
     result = convert.get("18S TJ 97100 03003")
     self.assertIsNotNone(result.get("geometry"))
     self.assertEqual(result.get("type"), "Feature")
     properties = result.get("properties")
     geometry = result.get("geometry")
     self.assertIsInstance(properties, dict)
     self.assertIsInstance(geometry, dict)
     self.assertEqual(geometry.get("type"), "Point")
     self.assertIsInstance(geometry.get("coordinates"), list)
Пример #7
0
    def convert_test_success(self, api_response):

        self.mock_requests.get(mockURL,
                               text=json.dumps(api_response),
                               status_code=200)
        convert = CoordinateConverter()
        result = convert.get("18S TJ 97100 03003")
        self.assertIsNotNone(result.get("geometry"))
        self.assertEqual(result.get("type"), "Feature")
        properties = result.get("properties")
        geometry = result.get("geometry")
        self.assertIsInstance(properties, dict)
        self.assertIsInstance(geometry, dict)
        self.assertEqual(geometry.get("type"), "Point")
        self.assertIsInstance(geometry.get("coordinates"), list)
Пример #8
0
    def test_convert_fail(self):
        convert_response_fail = {
            "properties": {
                "name": "12UUA844",
                "from": "mgrs",
                "to": "decdeg"
            }
        }

        with self.assertRaises(Exception):
            self.mock_requests.get(mockURL,
                                   text=json.dumps(convert_response_fail),
                                   status_code=500)
            CoordinateConverter().get_data()
Пример #9
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")
Пример #10
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")