How to use the mgrs.toMgrs 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 / coordinateConverter.py View on Github external
s = formatDmsString(pt4326.y(), pt4326.x(), 0, settings.converterDmsPrec, self.inputXYOrder,
                    settings.converterDelimiter, settings.converterAddDmsSpace)
            self.dmsLineEdit.setText(s)
        if id != 4:  # D M.MM'
            s = formatDmsString(pt4326.y(), pt4326.x(), 2, settings.converterDmmPrec, self.inputXYOrder,
                    settings.converterDelimiter, settings.converterAddDmsSpace)
            self.dmLineEdit.setText(s)
        if id != 5:  # DDMMSS
            s = formatDmsString(pt4326.y(), pt4326.x(), 1, settings.converterDmsPrec, self.inputXYOrder, settings.converterDdmmssDelimiter)
            self.ddmmssLineEdit.setText(s)
        if id != 6:  # UTM
            s = latLon2UtmString(pt4326.y(), pt4326.x(), settings.converterUtmPrec)
            self.utmLineEdit.setText(s)
        if id != 7:  # MGRS
            try:
                s = mgrs.toMgrs(pt4326.y(), pt4326.x())
            except Exception:
                s = 'Invalid'
            self.mgrsLineEdit.setText(s)
        if id != 8:  # Plus Codes
            try:
                s = olc.encode(pt4326.y(), pt4326.x(), settings.converterPlusCodeLength)
            except Exception:
                s = 'Invalid'
            self.plusLineEdit.setText(s)
        if id != 9: # GEOHASH
            try:
                s = geohash.encode(pt4326.y(), pt4326.x(), settings.converterGeohashPrecision)
            except Exception:
                s = 'Invalid'
            self.geohashLineEdit.setText(s)
github NationalSecurityAgency / qgis-latlontools-plugin / copyLatLonTool.py View on Github external
if self.settings.coordOrder == self.settings.OrderYX:
                    msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt.y(), delimiter, pt.x(), prec=self.settings.decimalDigits)
                else:
                    msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt.x(), delimiter, pt.y(), prec=self.settings.decimalDigits)
            else:
                msg = 'POINT({:.{prec}f} {:.{prec}f})'.format(pt.x(), pt.y(), prec=self.settings.decimalDigits)
        elif self.settings.captureProjIsMGRS():
            # Make sure the coordinate is transformed to EPSG:4326
            canvasCRS = self.canvas.mapSettings().destinationCrs()
            if canvasCRS == epsg4326:
                pt4326 = pt
            else:
                transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
                pt4326 = transform.transform(pt.x(), pt.y())
            try:
                msg = mgrs.toMgrs(pt4326.y(), pt4326.x())
            except Exception:
                # traceback.print_exc()
                msg = None
        elif self.settings.captureProjIsPlusCodes():
            # Make sure the coordinate is transformed to EPSG:4326
            canvasCRS = self.canvas.mapSettings().destinationCrs()
            if canvasCRS == epsg4326:
                pt4326 = pt
            else:
                transform = QgsCoordinateTransform(canvasCRS, epsg4326, QgsProject.instance())
                pt4326 = transform.transform(pt.x(), pt.y())
            try:
                msg = olc.encode(pt4326.y(), pt4326.x(), self.settings.plusCodesLength)
            except Exception:
                msg = None
        elif self.settings.captureProjIsUTM():
github NationalSecurityAgency / qgis-latlontools-plugin / geom2field.py View on Github external
msg = formatDmsString(pt.y(), pt.x(), 0, dmsPrecision, coordOrder, delimiter, use_dms_space)
                        elif wgs84Format == 2:  # D M.MM
                            msg = formatDmsString(pt.y(), pt.x(), 2, dmsPrecision, coordOrder, delimiter, use_dms_space)
                        else:  # DDMMSS
                            msg = formatDmsString(pt.y(), pt.x(), 1, dmsPrecision, coordOrder, delimiter, use_dms_space)
                    else:
                        if coordOrder == 0:
                            msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt.y(), delimiter, pt.x(), prec=decimalPrecision)
                        else:
                            msg = '{:.{prec}f}{}{:.{prec}f}'.format(pt.x(), delimiter, pt.y(), prec=decimalPrecision)
                elif outputFormat == 2:  # GeoJSON
                    msg = '{{"type": "Point","coordinates": [{:.{prec}f},{:.{prec}f}]}}'.format(pt.x(), pt.y(), prec=decimalPrecision)
                elif outputFormat == 3:  # WKT
                    msg = 'POINT({:.{prec}f} {:.{prec}f})'.format(pt.x(), pt.y(), prec=decimalPrecision)
                elif outputFormat == 4:  # MGRS
                    msg = mgrs.toMgrs(pt.y(), pt.x(), 5)
                elif outputFormat == 5:  # Plus codes
                    msg = olc.encode(pt.y(), pt.x(), plusCodesLength)
                elif outputFormat == 6:  # Geohash
                    msg = geohash.encode(pt.y(), pt.x(), geohashPrecision)
                else:  # WGS 84 UTM
                    msg = latLon2UtmString(pt.y(), pt.x(), dmsPrecision)
            except Exception:
                msg = ''

            f = QgsFeature()
            f.setGeometry(feature.geometry())
            if outputFormat == 0:  # Two fields for coordinates
                f.setAttributes(feature.attributes() + [msg, msg2])
            else:
                f.setAttributes(feature.attributes() + [msg])
            sink.addFeature(f)
github NationalSecurityAgency / qgis-latlontools-plugin / tomgrs.py View on Github external
# If the layer is not EPSG:4326 we need to convert it.
        epsg4326 = QgsCoordinateReferenceSystem('EPSG:4326')
        if layerCRS != epsg4326:
            transform = QgsCoordinateTransform(layerCRS, epsg4326, QgsProject.instance())

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

        iterator = source.getFeatures()
        for cnt, feature in enumerate(iterator):
            if feedback.isCanceled():
                break
            pt = feature.geometry().asPoint()
            if layerCRS != epsg4326:
                pt = transform.transform(pt)
            try:
                msg = mgrs.toMgrs(pt.y(), pt.x(), precision)
            except Exception:
                msg = ''
            f = QgsFeature()
            f.setGeometry(feature.geometry())
            f.setAttributes(feature.attributes() + [msg])
            sink.addFeature(f)
            if cnt % 100 == 0:
                feedback.setProgress(int(cnt * total))

        return {self.PrmOutputLayer: dest_id}

mgrs

MGRS coordinate conversion for Python

MIT
Latest version published 5 months ago

Package Health Score

71 / 100
Full package analysis