-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshp2i2s.py
executable file
·112 lines (95 loc) · 4.47 KB
/
shp2i2s.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python3
"""
@brief:
Convertir (et translater) un ensemble de polylignes ouvertes shp en i2s
@features:
* choix du nombre de chiffres significatifs à écrire (option `--digits`)
* translation possible (option `--shift`)
* La valeur de chaque polyligne est adaptable (option `--value`) :
*# si l'option value est un flottant alors les valeurs sont toutes égales à cette constante
*# si l'option value est égale à `iter` alors la valeur correspond à la position/numérotation de la polyligne
*# sinon la valeur est prise dans l'attribut portant le nom de l'option
* Chaque polyligne peut être échantionnée selon une distance maximale avec l'option `--ech`
"""
#FIXME: échantionner après la translation (gagner en précision)
import fiona
from shapely.geometry import LineString
import shapely.affinity as aff
import sys
from common.arg_command_line import myargparse
from geom.dataset import BlueKenueWrite_i2s
from geom.base import get_attr_value, resampling
parser = myargparse(description=__doc__)
parser.add_argument("inname", help="fichier d'entrée shp")
parser.add_argument("outname", help="fichier de sortie i2s (format BlueKenue)")
parser.add_argument("--value", help="colonne de la table attributaire (voir l'aide)", default=0)
parser.add_argument("--ech", help="colonne de la table attributaire (voir l'aide)")
parser.add_argument("--digits", type=int, help="nombre de chiffres significatifs des flottants")
parser.add_argument("--shift", type=float, nargs=2, help="décalage en X et Y (en mètre)")
parser.add_argument("--force", "-f", help="Ecrase le fichier de sortie s'il existe", action='store_true')
args = parser.parse_args()
try:
float(args.value)
value_type = 'int'
except ValueError:
value_type = args.value
ech_int = False
if args.ech is not None:
try:
float(args.ech)
ech_int = True
except ValueError:
pass
count = 0
with fiona.open(args.inname, 'r') as filein:
with BlueKenueWrite_i2s(args.outname, args.force, args.digits) as out_i2s:
out_i2s.auto_keywords()
out_i2s.write_header()
def write_single_polyline(coord, value, dist):
global count
"""Ecrire une seule polyligne à partir d'une liste de coordonnées"""
coord_2d = [c[:2] for c in coord] # ignore Z if present
if args.ech is not None:
coord_2d = resampling(coord_2d, dist)
linestring = LineString(coord_2d)
print("Polyligne {} avec {} points".format(count, len(coord_2d)))
count += 1
if args.shift is not None:
linestring = aff.translate(linestring, xoff=args.shift[0], yoff=args.shift[1])
out_i2s.write_polyline(linestring, value)
for i, obj in enumerate(filein):
if obj['geometry'] is None:
sys.exit("Object {} is None".format(i))
else:
obj_type = obj['geometry']['type']
coord = obj['geometry']['coordinates']
# Lecture distance si re-echantionnage si nécessaire
dist = None
if args.ech is not None:
if ech_int:
dist = float(args.ech)
else:
dist = get_attr_value(obj, args.ech)
# Récupération l'attribut value
if value_type == 'int':
value = float(args.value)
elif value_type == 'iter':
value = i
else:
value = get_attr_value(obj, args.value)
# Export des données
if obj_type == 'MultiLineString':
print("Un objet {} MutliLineString est converti en {} objets LineString".format(i, len(coord)))
for sub_coord in coord:
write_single_polyline(sub_coord, value, dist)
elif obj_type == 'LineString':
write_single_polyline(coord, value, dist)
elif obj_type == 'Polygon':
if len(coord)== 1: #FIXME: Astuce selon le formatage du fichier
coord = coord[0]
write_single_polyline(coord, value, dist)
else: # MultiPolygon
for sub_coord in coord:
write_single_polyline(sub_coord, value, dist)
else:
sys.exit("ERREUR: L'objet n'est pas une polyligne, mais vaut {}".format(obj_type))