How to use the mgrs.toWgs function in mgrs

To help you get started, we’ve selected a few mgrs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NationalSecurityAgency / qgis-latlontools-plugin / digitizer.py View on Github external
pt = geom.asPoint()
                    lat = pt.y()
                    lon = pt.x()
                elif re.search(r'POINT\(', text) is not None:
                    m = re.findall(r'POINT\(\s*([+-]?\d*\.?\d*)\s+([+-]?\d*\.?\d*)', text)
                    if len(m) != 1:
                        raise ValueError('Invalid Coordinates')
                    lon = float(m[0][0])
                    lat = float(m[0][1])
                else:
                    lat, lon = parseDMSString(text, self.inputXYOrder)
                srcCrs = epsg4326
            elif self.inputProjection == 1:
                # This is an MGRS coordinate
                text = re.sub(r'\s+', '', text)  # Remove all white space
                lat, lon = mgrs.toWgs(text)
                srcCrs = epsg4326
            elif self.inputProjection == 4:
                text = text.strip()
                coord = olc.decode(text)
                lat = coord.latitudeCenter
                lon = coord.longitudeCenter
                srcCrs = epsg4326
            elif self.inputProjection == 5:
                text = text.strip()
                pt = utmString2Crs(text, epsg4326)
                lat = pt.y()
                lon = pt.x()
                srcCrs = epsg4326
            else:  # Is either the project or custom CRS
                if re.search(r'POINT\(', text) is None:
                    coords = re.split(r'[\s,;:]+', text, 1)
github NationalSecurityAgency / qgis-latlontools-plugin / multizoom.py View on Github external
def addSingleCoord(self):
        '''Add a coordinate from the coordinate text box.'''
        parts = [x.strip() for x in self.addLineEdit.text().split(',')]
        label = ''
        data = []
        numFields = len(parts)
        try:
            if self.settings.multiZoomToProjIsMGRS():
                '''Check to see if we have an MGRS coordinate for entry'''
                lat, lon = mgrs.toWgs(re.sub(r'\s+', '', parts[0]))
                if numFields >= 2:
                    label = parts[1]
                if numFields >= 3:
                    data = parts[2:]
            elif self.settings.multiZoomToProjIsPlusCodes():
                coord = olc.decode(parts[0])
                lat = coord.latitudeCenter
                lon = coord.longitudeCenter
                if numFields >= 2:
                    label = parts[1]
                if numFields >= 3:
                    data = parts[2:]
            elif self.settings.multiZoomToProjIsUtm():
                pt = utmString2Crs(parts[0])
                lat = pt.y()
                lon = pt.x()
github NationalSecurityAgency / qgis-latlontools-plugin / field2geom.py View on Github external
if len(coords) < 2:
                            raise ValueError('Invalid Coordinates')
                        lat = float(coords[0])
                        lon = float(coords[1])
                elif field_type == 2:  # Lon (x), Lat (y)
                    if input_crs == epsg4326:
                        lat, lon = parseDMSString(attr1, 1)
                    else:
                        coords = re.split(r'[\s,;:]+', attr1, 1)
                        if len(coords) < 2:
                            raise ValueError('Invalid Coordinates')
                        lon = float(coords[0])
                        lat = float(coords[1])
                elif field_type == 3:  # MGRS
                    m = re.sub(r'\s+', '', str(attr1))  # Remove all white space
                    lat, lon = mgrs.toWgs(m)
                elif field_type == 4:  # Plus codes
                    coord = olc.decode(attr1)
                    lat = coord.latitudeCenter
                    lon = coord.longitudeCenter
                elif field_type == 5:  # Geohash
                    (lat, lon) = geohash.decode(attr1)
                    lat = float(lat)
                    lon = float(lon)
                elif field_type == 6:  # UTM
                    pt = utmString2Crs(attr1)
                    lat = pt.y()
                    lon = pt.x()

                f = QgsFeature()
                f.setAttributes(feature.attributes())
                f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(lon, lat)))
github NationalSecurityAgency / qgis-latlontools-plugin / zoomToLatLon.py View on Github external
geom = fet[0].geometry()
                if geom.isEmpty() or (geom.wkbType() != QgsWkbTypes.Point):
                    raise ValueError('Invalid GeoJSON Geometry')
                pt = geom.asPoint()
                return(pt.y(), pt.x(), epsg4326)

            # Check to see if it is standard UTM
            if isUtm(text):
                pt = utmString2Crs(text)
                return(pt.y(), pt.x(), epsg4326)

            # Check to see if it is an MGRS coordinate
            try:
                text2 = re.sub(r'\s+', '', str(text))
                lat, lon = mgrs.toWgs(text2)
                return(lat, lon, epsg4326)
            except Exception:
                pass

            # Check to see if it is a plus codes string
            try:
                coord = olc.decode(text)
                lat = coord.latitudeCenter
                lon = coord.longitudeCenter
                return(lat, lon, epsg4326)
            except Exception:
                pass

            # Check to see if it is a WKT POINT format
            if re.search(r'POINT\(', text) is not None:
                m = re.findall(r'POINT\(\s*([+-]?\d*\.?\d*)\s+([+-]?\d*\.?\d*)', text)
github NationalSecurityAgency / qgis-latlontools-plugin / coordinateConverter.py View on Github external
def commitMgrs(self):
        text = self.mgrsLineEdit.text().strip()
        text = re.sub(r'\s+', '', text)  # Remove all white space
        try:
            lat, lon = mgrs.toWgs(text)
            pt = QgsPoint(lon, lat)
            self.updateCoordinates(7, pt, epsg4326)
        except Exception:
            self.showInvalid(7)
github NationalSecurityAgency / qgis-latlontools-plugin / zoomToLatLon.py View on Github external
def convertCoordinate(self, text):
        try:
            if self.settings.zoomToProjIsMGRS():
                # An MGRS coordinate only format has been specified. This will result in an exception
                # if it is not a valid MGRS coordinate
                text2 = re.sub(r'\s+', '', str(text))  # Remove all white space
                lat, lon = mgrs.toWgs(text2)
                return(lat, lon, epsg4326)

            if self.settings.zoomToProjIsPlusCodes():
                # A Plus Codes coordinate has been selected. This will result in an exception
                # if it is not a valid plus codes coordinate.
                coord = olc.decode(text)
                lat = coord.latitudeCenter
                lon = coord.longitudeCenter
                return(lat, lon, epsg4326)

            if self.settings.zoomToProjIsStandardUtm():
                # A Standard UTM coordinate has been selected. This will result in an exception
                # if it is not a valid utm coordinate.
                pt = utmString2Crs(text)
                return(pt.y(), pt.x(), epsg4326)
github NationalSecurityAgency / qgis-latlontools-plugin / mgrstogeom.py View on Github external
(sink, dest_id) = self.parameterAsSink(
            parameters, self.PrmOutputLayer,
            context, source.fields(), QgsWkbTypes.Point, epsg4326)

        featureCount = source.featureCount()
        total = 100.0 / featureCount if featureCount else 0
        badFeatures = 0

        iterator = source.getFeatures()
        for cnt, feature in enumerate(iterator):
            if feedback.isCanceled():
                break
            m = feature[mgrsfieldname]
            try:
                m = re.sub(r'\s+', '', str(m))  # Remove all white space
                lat, lon = mgrs.toWgs(m)
            except Exception:
                # traceback.print_exc()
                badFeatures += 1
                continue
            f = QgsFeature()
            f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(lon, lat)))
            f.setAttributes(feature.attributes())
            sink.addFeature(f)
            if cnt % 100 == 0:
                feedback.setProgress(int(cnt * total))

        if badFeatures > 0:
            msg = "{} out of {} features contained MGRS coordinates".format(featureCount - badFeatures, featureCount)
            feedback.pushInfo(msg)

        return {self.PrmOutputLayer: dest_id}

mgrs

MGRS coordinate conversion for Python

MIT
Latest version published 4 months ago

Package Health Score

71 / 100
Full package analysis