Skip to content

Carto #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
18 changes: 17 additions & 1 deletion gpao/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
63 changes: 63 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")