Esempio n. 1
0
    def validate_edited_aoi(self):
        json_txt = self.leAOI.text()
        if not json_txt:
            self.reset_aoi_box()
            log.debug('No AOI defined, skipping validation')
            return

        try:
            json_obj = json.loads(json_txt)
        except ValueError:
            # noinspection PyUnresolvedReferences
            self._show_message('AOI GeoJSON is invalid',
                               level=Qgis.Warning,
                               duration=10)
            return

        json_geom = geometry_from_json(json_obj)

        if not json_geom:
            # noinspection PyUnresolvedReferences
            self._show_message('AOI GeoJSON geometry invalid',
                               level=Qgis.Warning,
                               duration=10)
            return

        geom: QgsGeometry = qgsgeometry_from_geojson(json_geom)
        self._aoi_box.setToGeometry(geom,
                                    QgsCoordinateReferenceSystem("EPSG:4326"))

        self.leAOI.blockSignals(True)
        self.leAOI.setText(json.dumps(json_geom))
        self.leAOI.blockSignals(False)

        self.zoom_to_aoi()
def test_geometry_from_json():
    # base case, no geometry
    assert None is utils.geometry_from_json({})
    # from an empty feature collection
    collection = {'type': 'FeatureCollection', 'features': []}
    assert None is utils.geometry_from_json(collection)

    # simple geometry, we're guessing by the type property w/ no further checks
    geom = {'type': 'Polygon'}
    assert geom == utils.geometry_from_json(geom)
    # from a feature
    feature = {'type': 'Feature', 'geometry': geom}
    assert geom == utils.geometry_from_json(feature)
    # from a feature collection
    collection = {'type': 'FeatureCollection', 'features': [feature]}
    assert geom == utils.geometry_from_json(collection)
def test_geometry_from_json():
    # base case, no geometry
    assert None is utils.geometry_from_json({})
    # from an empty feature collection
    collection = {'type': 'FeatureCollection', 'features': []}
    assert None is utils.geometry_from_json(collection)

    # simple geometry, we're guessing by the type property and presence of
    # the coordinates property
    geom = {'type': 'Polygon', 'coordinates': [1, 2]}
    assert geom == utils.geometry_from_json(geom)
    # from a feature
    feature = {'type': 'Feature', 'geometry': geom}
    assert geom == utils.geometry_from_json(feature)
    # from a feature collection
    collection = {'type': 'FeatureCollection', 'features': [feature]}
    assert geom == utils.geometry_from_json(collection)
def test_geometry_from_json():
    # base case, no geometry
    assert None is utils.geometry_from_json({})
    # from an empty feature collection
    collection = {'type': 'FeatureCollection', 'features': []}
    assert None is utils.geometry_from_json(collection)

    # simple geometry, we're guessing by the type property and presence of
    # the coordinates property
    geom = {'type': 'Polygon', 'coordinates': [1, 2]}
    assert geom == utils.geometry_from_json(geom)
    # from a feature
    feature = {'type': 'Feature', 'geometry': geom}
    assert geom == utils.geometry_from_json(feature)
    # from a feature collection
    collection = {'type': 'FeatureCollection', 'features': [feature]}
    assert geom == utils.geometry_from_json(collection)
Esempio n. 5
0
 def convert(self, val, param, ctx):
     val = read(val)
     if not val:
         return []
     try:
         geoj = json.loads(val)
     except ValueError:
         raise click.BadParameter('invalid GeoJSON')
     geom = geometry_from_json(geoj)
     if geom is None:
         raise click.BadParameter('unable to find geometry in input')
     return [filters.geom_filter(geom)]
Esempio n. 6
0
 def convert(self, val, param, ctx):
     val = read(val)
     if not val:
         return []
     try:
         geoj = json.loads(val)
     except ValueError:
         raise click.BadParameter('invalid GeoJSON')
     geom = geometry_from_json(geoj)
     if geom is None:
         raise click.BadParameter('unable to find geometry in input')
     return [filters.geom_filter(geom)]
Esempio n. 7
0
def geometry_from_json_str_or_obj(
        json_type: Union[str, dict]) -> Optional[dict]:
    """
    :param json_type: GeoJSON feature, feature collection or geometry as
    string or `json` object
    :type json_type: str | dict
    :rtype: dict | None
    """
    json_obj = json_str_or_obj_to_obj(json_type)

    # Strip outer Feature or FeatureCollection
    json_geom = geometry_from_json(json_obj)

    if not json_geom:
        log.debug('GeoJSON geometry invalid')

    return json_geom
Esempio n. 8
0
    def load_aoi_from_file(self):
        path, _ = QFileDialog.getOpenFileName(self, "Open GeoJSON AOI file",
                                              QDir.homePath(),
                                              "JSON (*.json);;All Files (*)")
        file = QFile(path)
        if not file.open(QFile.ReadOnly | QFile.Text):
            return

        inf = QTextStream(file)
        json_txt = inf.readAll()

        try:
            json_obj = json.loads(json_txt)
        except ValueError:
            # noinspection PyUnresolvedReferences
            self._show_message('GeoJSON from file invalid',
                               level=Qgis.Warning,
                               duration=10)
            return

        json_geom = geometry_from_json(json_obj)

        if not json_geom:
            # noinspection PyUnresolvedReferences
            self._show_message('GeoJSON geometry from file invalid',
                               level=Qgis.Warning,
                               duration=10)
            return

        geom: QgsGeometry = qgsgeometry_from_geojson(json_geom)
        self._aoi_box.setToGeometry(geom,
                                    QgsCoordinateReferenceSystem("EPSG:4326"))

        self.leAOI.setText(json.dumps(json_geom))

        self.zoom_to_aoi()