def test_hent_observationer_naer_geometri(firedb: FireDb): go = firedb.hent_geometri_objekt( punktid="67e3987a-dc6b-49ee-8857-417ef35777af") os = firedb.hent_observationer_naer_geometri(go.geometri, 10000) assert len(os) == 68 point = Geometry("POINT (10.2112609352788 56.1567354902778)") os = firedb.hent_observationer_naer_geometri(point, 100) assert len(os) == 6 polygon = Geometry(("POLYGON ((10.209 56.155, " "10.209 56.158, " "10.215 56.158, " "10.215 56.155, " "10.209 56.155))")) os = firedb.hent_observationer_naer_geometri(polygon, 100) assert len(os) == 10
def test_geometry(): g_wkt = Geometry(WKT_POINT) assert g_wkt.wkt == WKT_POINT assert str(g_wkt) == WKT_POINT assert g_wkt.__geo_interface__ == DICT_POINT g_dict = Geometry(DICT_POINT) assert g_dict.wkt == WKT_POINT assert str(g_dict) == WKT_POINT assert g_dict.__geo_interface__ == DICT_POINT with pytest.raises(TypeError): Geometry([])
def klargør_geometrifiler(geometrifiler: Iterable[str]) -> List[Geometry]: """ Returnerer samlet liste med hvert lag i hver fil. Hver geometrifil kan have flere features eller lag. Åbn og konvertér indhold af geometrifiler. """ klargjorte_geometrier: List[Geometry] = [] for filnavn in geometrifiler: geometri_data = fiona.open(filnavn) # Validér crs = geometri_data.crs.get("init") if crs.upper() != SRID.WGS84: continue # Konvertér indhold til shapely-objekter delgeometrier = [ geometry.shape(delgeometri.get("geometry")) for delgeometri in geometri_data ] # Opret Geometry-instanser klargjorte_geometrier.extend( [Geometry(dgb.wkt) for dgb in delgeometrier]) geometri_data.close() return klargjorte_geometrier
def write( output, geometri, geometrifil, buffer, fra, til, fixpunkter, fixpunkterfil, parameterfil, ): """Create a gama input file""" writer = GamaWriter(firedb, output) g = None if geometrifil is None and geometri is None: writer.take_all_points() else: if geometri is not None: g = Geometry(geometri) else: wkt = geometrifil.read() g = Geometry(wkt) if fra is not None and til is not None: observations = firedb.hent_observationer_naer_geometri( g, buffer, fra, til) else: observations = firedb.hent_observationer_naer_geometri(g, buffer) writer.take_observations(observations) if fixpunkter is not None or fixpunkterfil is not None: if fixpunkter is not None: fixpunkter_literal = fixpunkter else: fixpunkter_literal = fixpunkterfil.read() fixpunkter_list = [ pkt.strip() for pkt in fixpunkter_literal.split(",") ] writer.set_fixed_point_ids(fixpunkter_list) parameters = configparser.ConfigParser() parameters.read(parameterfil) writer.write(True, False, "Created by fire-gama", parameters)
def test_naer_geometry(firedb: FireDb, tmp_path: Path): outfile = tmp_path / "near_geometry.xml" with open(outfile, "w") as output: writer = GamaWriter(firedb, output) g = Geometry("POINT (10.200000 56.100000)") observations = firedb.hent_observationer_naer_geometri(g, 10000) writer.take_observations(observations) parameters = configparser.ConfigParser() parameters.read("fire-gama.ini") writer.write(True, False, "test_near_geometry", parameters) os.remove(outfile)
def polygoner(punkter: Iterable[Punkt], buffer: int) -> Iterator[Geometry]: """Returnerer en søgeklar liste med Geometry-instanser til søgning i databasen.""" # Hent punkternes WGS84-koordinater: # Geometri-koordinaterne er altid i WGS84. koordinatsæt = (punkt.geometri.koordinater for punkt in punkter) # Opbyg geometri for punkt-koordinater til søgning. shapely_punkter = (geometry.Point(*koordinater) for koordinater in koordinatsæt) # Lav den endelige søge-geometri ved at bruge den angivne buffer som # radius i en forsimplet cirkel (polygon) omkring koordinaterne. shapely_polygoner = (punkt.buffer(buffer) for punkt in shapely_punkter) # Tilføj polygonerne for de enkelte identer til geometrier, der skal søges i nærheden af. # Opret samtidig et geometri-objekt med hver søge-geometris Well-Known Text (WKT). return (Geometry(polygon.wkt) for polygon in shapely_polygoner)
def test_naer_geometry_time_interval(firedb: FireDb, tmp_path: Path): outfile = tmp_path / "near_geometry_time_interval.xml" output = open(outfile, "w") writer = GamaWriter(firedb, output) g = Geometry("POINT (10.200000 56.100000)") observations = firedb.hent_observationer_naer_geometri( g, 10000, datetime.datetime(2015, 10, 8), datetime.datetime(2018, 10, 9)) writer.take_observations(observations) parameters = configparser.ConfigParser() parameters.read("fire-gama.ini") writer.write(True, False, "test_near_geometry_time_interval", parameters) output.close os.remove(outfile)
def test_in_polygon(firedb: FireDb, tmp_path: Path): outfile = tmp_path / "in_polygon.xml" with open(outfile, "w") as output: writer = GamaWriter(firedb, output) geometry = Geometry(("POLYGON ((10.209 56.155, " "10.209 56.158, " "10.215 56.158, " "10.215 56.155, " "10.209 56.155))")) observations = firedb.hent_observationer_naer_geometri(geometry, 5000) writer.take_observations(observations) writer.set_fixed_point_ids(["K-63-09946"]) parameters = configparser.ConfigParser() parameters.read("fire-gama.ini") writer.write(True, False, "test_in_polygon", parameters) os.remove(outfile)
def test_in_polygon(firedb: FireDb, tmp_path: Path): outfile = tmp_path / "in_polygon.xml" output = open(outfile, "w") writer = GamaWriter(firedb, output) geometry = Geometry(("POLYGON ((10.209 56.155, " "10.209 56.158, " "10.215 56.158, " "10.215 56.155, " "10.209 56.155))")) observations = firedb.hent_observationer_naer_geometri(geometry, 5000) writer.take_observations(observations) writer.set_fixed_point_ids(["67e3987a-dc6b-49ee-8857-417ef35777af"]) parameters = configparser.ConfigParser() parameters.read("fire-gama.ini") writer.write(True, False, "test_in_polygon", parameters) output.close os.remove(outfile)
def processAlgorithm(self, parameters, context, feedback: QgsProcessingFeedback): # Input / Output source = self.parameterAsSource(parameters, self.INPUT, context) (sink, dest_id) = self.create_output_sink(parameters, context, source.sourceCrs()) # Filter parameters observation_type_indices = self.parameterAsEnums( parameters, self.OBSERVATION_TYPE, context) observation_types = list( map(lambda i: self.OBSERVATION_TYPES[i][0], observation_type_indices)) from_date = None from_date_string = self.parameterAsString(parameters, self.FROM_DATE, context) if from_date_string: from_date = datetime.fromisoformat(from_date_string) to_date = None to_date_string = self.parameterAsString(parameters, self.TO_DATE, context) if to_date_string: to_date = datetime.fromisoformat(to_date_string) fire_connection_string = self.settings.value("fire_connection_string") fireDb = FireDb(fire_connection_string, debug=True) features = list(source.getFeatures()) total_num_features = len(features) total_num_features_processed = 0 # for current, feature in enumerate(features): for feature in features: if feedback.isCanceled(): return {} wkt = feature.geometry().asWkt().upper() geometry = Geometry(wkt) observations = fireDb.hent_observationer_naer_geometri( geometri=geometry, afstand=0, tidfra=from_date, tidtil=to_date) pid_list = self.get_pids_from_observations(observations) geometriobjekter = self.get_geometriobjekter_from_pids( fireDb, pid_list) idents = self.get_idents_from_pids(fireDb, pid_list) feedback.setProgressText( "Fandt {antal} observationer".format(antal=len(observations))) feedback.setProgressText("Fandt {antal} geometriobjekter".format( antal=len(geometriobjekter))) feedback.setProgressText( "Fandt {antal} idents".format(antal=len(idents))) for current, observation in enumerate(observations): observation_type_id = observation.observationstypeid if observation_type_id in observation_types: feature = self.create_feature_from_observation( observation, geometriobjekter, idents, feedback) if feature: sink.addFeature(feature, QgsFeatureSink.FastInsert) total_num_features_processed = total_num_features_processed + 1 feedback.setProgress(total_num_features_processed / total_num_features) if feedback.isCanceled(): return {} apply_theme = self.parameterAsBool(parameters, self.APPLY_THEME, context) if apply_theme: style_file = os.path.join(os.path.dirname(__file__), "..", "styles", "observation.qml") alg_params = {"INPUT": dest_id, "STYLE": style_file} processing.run( "qgis:setstyleforvectorlayer", alg_params, context=context, feedback=feedback, is_child_algorithm=True, ) return {self.OUTPUT: dest_id}
istedet med dette script. """ from pathlib import Path from sqlalchemy.sql import text import fiona from fire.api import FireDb from fire.api.model import Geometry firedb = FireDb(db="ci") with fiona.open(Path(__file__).parent / "../data/herredsogn.shp") as herredsogn: for feature in herredsogn: g = Geometry(feature["geometry"]) kode = feature["properties"]["kode"] # Fjollet hack for at omgå begrænsninger i Oracle. # Tilsyneladende må strenge ikke være længere end 4000 tegn, så her omgår vi begrænsningen # ved at splitte wkt-strengen i to dele og sammensætte den med ||-operatoren i SQL udtrykket. # Idioti, men det virker. wkt1, wkt2 = g.wkt[:len(g.wkt) // 2], g.wkt[len(g.wkt) // 2:] statement = text(f"""INSERT INTO herredsogn (kode, geometri) VALUES ('{kode}', SDO_GEOMETRY(TO_CLOB('{wkt1}') || TO_CLOB('{wkt2}'), 4326) )""") firedb.session.execute(statement) firedb.session.commit() print("Herred- og sognegrænser tilføjet databasen")