diff --git a/README.md b/README.md index 27cb57c..40c504f 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,54 @@ Cet exemple sauvegarde un fichier `project.json` avec le contenu suivant : } ``` +### Création de jobs avec attribut geometry + +On peut définir une géométrie pour chaque job avec l'attribut geometry. Dans la base de données elle est définie en WGS84 (epsg:4326). + +Cette geometrie peut-être renseignée en : +* binaire, exemple : +``` python +geom1 = "0106000020E61000000100000001030000000100000005000000500834AD72D51540CB389A27B9084740C28E5BF782D715401DA21CCD7B0E474072E2A118C5191640D6667F3B4D0E4740D9D3A6EEA6171640108BB99F8A084740500834AD72D51540CB389A27B9084740" +job_with_geom1 = Job("job_with_geom", "sleep 5", geometry=geom1) +``` +* texte, exemples : +``` python +geom2 = 'SRID=2154;POINT(736000 6917000)' +job_with_geom2 = Job("job_with_geom", "sleep 5", geometry=geom2) + +geom3 = "SRID=2154;POLYGON((736000 6917000 , 737000 6917000 , 737000 6918000 , 736000 6918000 , 736000 6917000))" +job_with_geom3 = Job("job_with_geom", "sleep 5", geometry=geom3) + +``` + +Elle peut aussi être définie à postériori avec une liste de coordonnées et le SRID, exemple : +* pour un point +``` python +job_with_geom4 = Job("job_with_geom", "sleep 5") +job_with_geom4.add_geometry_from_coordinates('2154', [(736000, 6917000)]) + +``` +* pour un polygone (triangle = 3 coords, carré = 4 coords, etc) +``` python +job_with_geom5 = Job("job_with_geom", "sleep 5") +job_with_geom5.add_geometry_from_coordinates('2154', [(736000, 6917000) , (737000, 6917000) , (737000, 6918000) , (736000, 6918000)]) +``` + +Ci-dessous, un exemple de fonction qui ajoute une geometrie à partir du nom des jobs suivants la règle d'appellation "Name_AAAA_XXXX_YYYYY_LA93_IGN69" : +``` python +def add_geometry_from_job_name(job): + job_name = job.name + job_name = job_name.split('_') + SRID = 4326 + if job_name[4]=='LA93': + SRID = 2154 + coords = [(job_name[2]+"000", job_name[3]+"000")] + job.add_geometry_from_coordinates(SRID, coords) + +job_with_geom6 = Job("Semis_2023_0736_6917_LA93_IGN69", "sleep 5") +add_geometry_from_job_name(job_with_geom6) +``` + ### Licence Ce projet est sous licence CECILL-B (voir [LICENSE.md](https://github.com/ign-gpao/.github/blob/main/LICENSE.md)). diff --git a/gpao/job.py b/gpao/job.py index 30d55d6..3236f61 100644 --- a/gpao/job.py +++ b/gpao/job.py @@ -8,7 +8,7 @@ class Job: cpt_id = 0 - def __init__(self, name, command, deps=None, tags=None): + def __init__(self, name, command, deps=None, tags=None, geometry=None): """ constructor""" self.name = name @@ -27,6 +27,8 @@ def __init__(self, name, command, deps=None, tags=None): if tags is not None: self.tags.extend(tags) + self.geometry = geometry + self.internal_id = Job.cpt_id Job.cpt_id += 1 @@ -46,6 +48,20 @@ def add_dependency(self, dep): else: self.deps.append(dep) + def add_geometry_from_coordinates(self, srid, coords): + """ Add a geometry to this job from coordinates list and SRID""" + geom = "" + if len(coords) == 1: + geom = f"SRID={srid};POINT({coords[0][0]} {coords[0][1]})" + elif len(coords) >= 3: + geom = f"SRID={srid};POLYGON((" + for coord in coords: + geom += f"{coord[0]} {coord[1]} , " + geom += f"{coords[0][0]} {coords[0][1]}))" + else: + raise RuntimeError("Error on coordinates") + self.geometry = geom + def to_json(self): """ convert to json string""" # del internal id from job diff --git a/test.py b/test.py index ba286f6..40ccdd7 100644 --- a/test.py +++ b/test.py @@ -77,3 +77,66 @@ def is_valid(json1, json2): builder.send_project_to_api("http://localhost:8080") else: print("Error, not same json") + + +# Exemple de jobs avec geometry + +# Les différentes manières de renseigner l'attribut geometry : + +# en binaire +geom1 = "0106000020E61000000100000001030000000100000005000000500834AD72D51540C" ++"B389A27B9084740C28E5BF782D715401DA21CCD7B0E474072E2A118C5191640D6667F3B4D0E4" ++"740D9D3A6EEA6171640108BB99F8A084740500834AD72D51540CB389A27B9084740" +job_with_geom1 = Job("job_with_geom", "sleep 5", geometry=geom1) + +# en texte +geom2 = 'SRID=2154;POINT(736000 6917000)' +job_with_geom2 = Job("job_with_geom", "sleep 5", geometry=geom2) + +geom3 = "SRID=2154;POLYGON((736000 6917000 , 737000 6917000 , 737000 6918000 " ++", 736000 6918000 , 736000 6917000))" +job_with_geom3 = Job("job_with_geom", "sleep 5", geometry=geom3) + +# À postériori avec une liste de coordonnées et le SRID + +# pour un point +job_with_geom4 = Job("job_with_geom", "sleep 5") +job_with_geom4.add_geometry_from_coordinates('2154', [(736000, 6917000)]) + +# pour un polygone (carré = 4 points) +job_with_geom5 = Job("job_with_geom", "sleep 5") +job_with_geom5.add_geometry_from_coordinates('2154', + [(736000, 6917000), + (737000, 6917000), + (737000, 6918000), + (736000, 6918000)]) + +# A partir d'une fonction que chacun prendra le soin d'écrire selon leurs +# regles d'appellation des jobs, exemple : + + +def add_geometry_from_job_name(job): + job_name = job.name + job_name = job_name.split('_') + SRID = 4326 + if job_name[4] == 'LA93': + SRID = 2154 + coords = [(job_name[2]+"000", job_name[3]+"000")] + job.add_geometry_from_coordinates(SRID, coords) + + +job_with_geom6 = Job("Semis_2023_0736_6917_LA93_IGN69", "sleep 5") +add_geometry_from_job_name(job_with_geom6) + + +project_map1 = Project("project-map1", [job_with_geom1, + job_with_geom2, + job_with_geom3]) +project_map2 = Project("project-map2", [job_with_geom4, job_with_geom5]) +project_map3 = Project("project-map3", [job_with_geom6]) + +builder = Builder([project_map1, project_map2, project_map3]) + +builder.save_as_json("project-map.json") + +builder.send_project_to_api("http://localhost:8080")