Example #1
0
    def post(self, request):
        request_proto = interop_admin_api_pb2.GpsConversionRequest()
        try:
            json_format.Parse(request.body, request_proto)
        except Exception as e:
            return HttpResponseBadRequest(
                'Failed to parse request. Error: %s' % str(e))

        if not request_proto.HasField(
                'latitude') or not request_proto.HasField('longitude'):
            return HttpResponseBadRequest('Request missing fields.')

        try:
            latlon = string2latlon(request_proto.latitude,
                                   request_proto.longitude, LATLON_FORMAT)
        except Exception as e:
            return HttpResponseBadRequest('Failed to convert GPS. Error: %s' %
                                          str(e))

        response = interop_admin_api_pb2.GpsConversionResponse()
        response.latitude = latlon.lat.decimal_degree
        response.longitude = latlon.lon.decimal_degree

        return HttpResponse(json_format.MessageToJson(response),
                            content_type="application/json")
Example #2
0
    def parse_map_coords_string(self, coords_string):
        split_string = coords_string.split(',')

        if "-" in split_string[0]:
            # dd mm ss.ss
            split_latlon = split_string[0].split(' ')
            lat_string = split_latlon[0].replace('N', '').replace('S', "-")
            lon_string = split_latlon[1].replace('£',
                                                 'E').replace('E', '').replace(
                                                     'W', "-")
            position = string2latlon(lat_string,
                                     lon_string,
                                     format_str="d%-%m%-%S")
        elif "°" not in split_string[0]:
            # mgrs
            mgrs_string = split_string[0].replace(" ", "")
            decoded_mgrs = mgrs.UTMtoLL(mgrs.decode(mgrs_string))
            position = LatLon(Latitude(degree=decoded_mgrs["lat"]),
                              Longitude(degree=decoded_mgrs["lon"]))
        else:
            raise ValueError(f"Invalid coordinate format: {split_string[0]}")

        elevation = split_string[1].replace(' ', '')
        if "ft" in elevation:
            elevation = int(elevation.replace("ft", ""))
        elif "m" in elevation:
            elevation = round(int(elevation.replace("m", "")) * 3.281)
        else:
            raise ValueError("Unable to parse elevation: " + elevation)

        self.captured_map_coords = str()
        self.logger.info("Parsed captured text: " + str(position))
        return position, elevation
Example #3
0
def test_LatLon_fromstring():
    '''
    Test LatLon method from_string
    '''
    lat_str, lon_str = '5.8833', '-162.0833' # test location is Palmyra Atoll
    # Convert decimal degrees string to LatLon object:
    palmyra = string2latlon(lat_str, lon_str, 'D')
    assert str(palmyra) == '5.8833, -162.0833' # Failure to convert from degree, minutes, second, hemisphere string
    lat_str, lon_str = '5, 52, 59.88, N', '162, 4, 59.88, W'
    # Convert degrees minutes second string with hemisphere identifier to LatLon object:
    palmyra = string2latlon(lat_str, lon_str, 'd%, %m%, %S%, %H')
    assert str(palmyra) == '5.8833, -162.0833' # Failure to convert from degree, minutes, second, hemisphere string
    lat_str, lon_str = 'N_5deg 52.998', 'W_162deg 4.998'
    # Convert degrees minutes second string with fancy separators to LatLon object:
    palmyra = string2latlon(lat_str, lon_str, 'H%_%d%deg %M')
    assert str(palmyra) == '5.8833, -162.0833' # Failure to convert from hemisphere, degree, minutes string
Example #4
0
 def get_degree_based_coord_data(self, match, pattern):
     """Takes degree based coords and turns them into LatLon object"""
     lat_lon = string2latlon(
         self.clean_dms_coords(match.group(1)),
         self.clean_dms_coords(match.group(2)),
         pattern
     )
     return lat_lon
Example #5
0
def get_latlon(coords):
    # Find boundary box with max 2 miles from coords
    r = re.search('([NS][ ]*[0-9]+°?[ ]*[0-9,.]+)[ ]+([WE][ ]*[0-9]+°?[ ]*[0-9,.]+)', coords)
    gc_lat = r.group(1)
    gc_lon = r.group(2)
    # print(gc_latlon.to_string('D'))
    # print(gc_latlon)
    return string2latlon(gc_lat, gc_lon, 'H% %d%° %M')
Example #6
0
def load_physicalwps():
    # wp_list = []
    gc_filename = 'geocheck.sqlite'
    conn_gc = sqlite3.connect(gc_filename)
    c = conn_gc.cursor()
    c.execute("SELECT wp_lat, wp_lon from physical_waypoints")
    wps = c.fetchall()
    conn_gc.close()
    return [string2latlon(str(w[0]), str(w[1]), 'd') for w in wps]

    '''
    def get_degree_based_coord_data(self, match, pattern):
        """
        Takes degree based coords and turns them into LatLon object

        :param match: the regex match for a degree coord set
        :type match: re.MatchObject
        :param pattern: the degree pattern we detected
        :type pattern: str
        :return: latlon object
        :rtype: LatLon
        """
        lat_lon = string2latlon(
            self.clean_dms_coords(match.group(1)),
            self.clean_dms_coords(match.group(2)),
            pattern
        )
        return lat_lon
Example #8
0
    def parse_map_coords_string(self, coords_string, tomcat_mode=False):
        coords_string = coords_string.upper()
        # "X-00199287 Z+00523070, 0 ft"   Not sure how to convert this yet

        # "37 T FJ 36255 11628, 5300 ft"  Tessaract did not like this one because the DCS font J looks too much like )
        res = re.match("^(\d+ [a-zA-Z] [a-zA-Z][a-zA-Z] \d+ \d+), (\d+) (FT|M)$", coords_string)
        if res is not None:
            mgrs_string = res.group(1).replace(" ", "")
            decoded_mgrs = mgrs.UTMtoLL(mgrs.decode(mgrs_string))
            position = LatLon(Latitude(degree=decoded_mgrs["lat"]), Longitude(
                degree=decoded_mgrs["lon"]))
            elevation = float(res.group(2))

            if res.group(3) == "M":
                elevation = elevation * 3.281

            return position, elevation

        # "N43°10.244 E40°40.204, 477 ft"  Degrees and decimal minutes
        res = re.match("^([NS])(\d+)°([^\s]+) ([EW])(\d+)°([^,]+), (\d+) (FT|M)$", coords_string)
        if res is not None:
            lat_str = res.group(2) + " " + res.group(3) + " " + res.group(1)
            lon_str = res.group(5) + " " + res.group(6) + " " + res.group(4)
            position = string2latlon(lat_str, lon_str, "d% %M% %H")
            elevation = float(res.group(7))

            if res.group(8) == "M":
                elevation = elevation * 3.281

            return position, elevation

        # "N42-43-17.55 E40-38-21.69, 0 ft" Degrees, minutes and decimal seconds
        res = re.match("^([NS])(\d+)-(\d+)-([^\s]+) ([EW])(\d+)-(\d+)-([^,]+), (\d+) (FT|M)$", coords_string)
        if res is not None:
            lat_str = res.group(2) + " " + res.group(3) + " " + res.group(4) + " " + res.group(1)
            lon_str = res.group(6) + " " + res.group(7) + " " + res.group(8) + " " + res.group(5)
            position = string2latlon(lat_str, lon_str, "d% %m% %S% %H")
            elevation = float(res.group(9))

            if res.group(10) == "M":
                elevation = elevation * 3.281

            return position, elevation

        # "43°34'37"N 29°11'18"E, 0 ft" Degrees minutes and seconds
        res = re.match("^(\d+)°(\d+)'([^\"]+)\"([NS]) (\d+)°(\d+)'([^\"]+)\"([EW]), (\d+) (FT|M)$", coords_string)
        if res is not None:
            lat_str = res.group(1) + " " + res.group(2) + " " + res.group(3) + " " + res.group(4)
            lon_str = res.group(5) + " " + res.group(6) + " " + res.group(7) + " " + res.group(8)
            position = string2latlon(lat_str, lon_str, "d% %m% %S% %H")
            elevation = float(res.group(9))

            if res.group(10) == "M":
                elevation = elevation * 3.281

            return position, elevation

        split_string = coords_string.split(',')

        if tomcat_mode:
            latlon_string = coords_string.replace("\\", "").replace("F", "")
            split_string = latlon_string.split(' ')
            lat_string = split_string[1]
            lon_string = split_string[3]
            position = string2latlon(
                lat_string, lon_string, format_str="d%°%m%'%S")

        if not tomcat_mode:
            elevation = split_string[1].replace(' ', '')
            if "ft" in elevation:
                elevation = int(elevation.replace("ft", ""))
            elif "m" in elevation:
                elevation = round(int(elevation.replace("m", ""))*3.281)
            else:
                raise ValueError("Unable to parse elevation: " + elevation)
        else:
            elevation = self.capture_map_coords(2074, 97, 966, 32)

        self.captured_map_coords = str()
        self.logger.info("Parsed captured text: " + str(position))
        return position, elevation
Example #9
0
 def get_degree_based_coord_data(self, match, pattern):
     """Takes degree based coords and turns them into LatLon object"""
     lat_lon = string2latlon(self.clean_dms_coords(match.group(1)),
                             self.clean_dms_coords(match.group(2)), pattern)
     return lat_lon
    def city_data(self, city_data):
        self.__city_data = city_data
        if self._page_request():
            try:
                city_data_table = self._get_xpath("//table[@id='background-image']/tbody")
                for tr in city_data_table:
                    try:
                        contents = [re.sub(r' \(\d+\)', '', str(i.lower()))
                                    for i in tr.xpath("./td/text()|./td/a/text()")[:-2]]
                        normal_url = str(tr.xpath("./td/a/@href")[0])
                    except Exception as e:
                        error_info = ErrorInfo()
                        self._error_log('BadXpath', error_info.file_name, error_info.line_number)
                        if self.print_errors:
                            print("Exception: {}, at line {}, file {}".format(e, error_info.line_number,
                                                                              error_info.file_name))
                        raise BadXpath(u'The page structure probably '
                                       u'has changed, please verify the source html.')
                    try:
                        lat_lon = (string2latlon(contents[1], contents[2], "d%°%m%'%h"))
                        contents[1], contents[2] = lat_lon.lon.decimal_degree, lat_lon.lat.decimal_degree
                    except:
                        raise BadXpath("wrong path")
                    try:
                        contents[3] = float(contents[3])
                    except:
                        contents[3] = None
                    content_dict = {"url": normal_url,
                                    "country": self.country,
                                    "city": contents[0],
                                    "point": contents[1:3],
                                    "altitude": contents[3],
                                    "climate": contents[4],
                                    "biome": contents[5]}

                    self.city_data.append(content_dict)
            except BadXpath:
                try:
                    data_ul = self._get_xpath("//ul")
                    geo_data = data_ul.xpath("./li[1]/text()|./li[1]/a/text()")[0]
                    geo_data = geo_data.split(",", 1)

                    point = re.findall(r"\d+°\d+'\S{1}", geo_data[1])
                    lat_lon = (string2latlon(point[0], point[1], "d%°%m%'%h"))

                    city = geo_data[0]
                    point = [lat_lon.lon.decimal_degree, lat_lon.lat.decimal_degree]
                    altitude = float(re.findall(r"(\d+)\s+?m\s+?.*?\(-?\d+ ft\)", geo_data[1])[0])
                    classification = data_ul.xpath("./li[2]/text()|./li[2]/b/text()")
                    classification = "".join(classification)

                    try:
                        biome = re.findall(r"has a (.*) climate \(", classification)[0].lower()
                    except:
                        biome = None

                    climate = re.findall(r"-Geiger classification: (\w+)\)", classification)[0].lower()

                    content_dict = {"url": self.url,
                                    "country": self.country,
                                    "city": city.lower(),
                                    "point": point,
                                    "altitude": altitude,
                                    "climate": climate,
                                    "biome": biome}
                    self.city_data.append(content_dict)

                    warnings.warn("This may be as city url", UserWarning)
                except:
                    self._error_log("BadXpath", "url_spider", 131)
                    print("Impossible to get the data")