864 | | Retour à l'[wikitr:/Help Aide Principale][[TranslatedPages(revision=17)]] |
865 | | |
866 | | = Greffon -> Scripting -> Python = |
867 | | == Objectif == |
868 | | D'autres exemples en Python : |
869 | | |
870 | | [[PageOutline(2,Table des Matières)]] |
871 | | |
872 | | |
873 | | == Convertir un chemin qui se raccorde à un carrefour giratoire non fractionné en une bifurcation. == |
874 | | Convertir un chemin qui se connecte à un carrefour giratoire en une fourche composée de 2 chemins à sens unique. En divisant le chemin sur son dernier nœud et en l'attachant aux 2 nœuds adjacents au nœud commun du chemin du carrefour giratoire. Cette version ne fonctionne que sur les carrefours giratoires non fractionnés. Voir l'exemple suivant pour une version mise à jour. |
875 | | |
876 | | {{{#!python |
877 | | from javax.swing import JOptionPane |
878 | | from org.openstreetmap.josm.gui import MainApplication |
879 | | |
880 | | import org.openstreetmap.josm.command as Command |
881 | | import org.openstreetmap.josm.data.osm.Way as Way |
882 | | |
883 | | editLayer = MainApplication.getLayerManager().getEditLayer() |
884 | | if editLayer and editLayer.data: |
885 | | selected_ways = editLayer.data.getSelectedWays() |
886 | | print selected_ways |
887 | | |
888 | | if not(selected_ways) or len(selected_ways)>1: |
889 | | JOptionPane.showMessageDialog(MainApplication.parent, |
890 | | "Veuillez sélectionner un chemin unique qui se raccorde à un carrefour giratoire") |
891 | | else: |
892 | | for way in selected_ways: |
893 | | if way.get('oneway') in ['yes', '-1']: |
894 | | JOptionPane.showMessageDialog(MainApplication.parent, "Ce chemin a un attribut - sens unique") |
895 | | else: |
896 | | node_before = None |
897 | | node_after = None |
898 | | common_node = None |
899 | | common_node_becomes_node_before=None |
900 | | #print dir(way) |
901 | | for fln in [way.firstNode(), way.lastNode()]: |
902 | | print 'fln', fln |
903 | | for parentway in fln.getParentWays(): |
904 | | if parentway.get("junction")=='roundabout': |
905 | | for n in parentway.getNodes(): |
906 | | if common_node: |
907 | | # nous avons trouvé un nœud commun entre le chemin sélectionné et |
908 | | # le chemin du carrefour giratoire dans l'itération précédente |
909 | | node_after = n |
910 | | # nous avons terminé ici |
911 | | break |
912 | | if n.getId() == fln.getId(): |
913 | | # c'est le nœud que le carrefour giratoire a en commun avec le chemin sélectionné |
914 | | common_node = n |
915 | | if not(node_before): |
916 | | # normalement, nous avons rencontré un nœud du carrefour giratoire |
917 | | # bien avant celui-ci, mais si le nœud commun est le premier nœud |
918 | | # d'un carrefour giratoire non divisé, nous devrons prendre le dernier |
919 | | # nœud du carrefour giratoire à la place |
920 | | node_before = parentway.getNodes()[-2] |
921 | | node_after = parentway.getNodes()[1] |
922 | | break |
923 | | # si ce n'est pas le cas, nous passons par la boucle une fois de plus pour mettre le prochain |
924 | | # nœud dans node_after |
925 | | continue |
926 | | node_before = n |
927 | | if common_node: |
928 | | # si common_node est déjà défini à ce stade, cela signifie qu'il était |
929 | | # le premier nœud du chemin sélectionné, |
930 | | # il devra donc être remplacé par node_before dans le chemin sélectionné |
931 | | common_node_becomes_node_before = True |
932 | | adjacent_node_to_split_on = way.getNodes()[1] |
933 | | break |
934 | | else: |
935 | | common_node_becomes_node_before = False |
936 | | adjacent_node_to_split_on = way.getNodes()[-1] |
937 | | if not(common_node) or common_node_becomes_node_before==None: |
938 | | JOptionPane.showMessageDialog(MainApplication.parent, |
939 | | "Veuillez sélectionner un chemin qui se raccorde à un carrefour giratoire") |
940 | | else: |
941 | | print common_node.get('name') |
942 | | print node_before.get('name') |
943 | | print node_after.get('name') |
944 | | |
945 | | commandsList = [] |
946 | | if len(way.getNodes())>2: |
947 | | # diviser le dernier segment avant le carrefour giratoire si nécessaire |
948 | | commandsList.append(Command.SplitWayCommand.split( |
949 | | way, [adjacent_node_to_split_on], [])) |
950 | | MainApplication.undoRedo.add(Command.SequenceCommand( |
951 | | "Séparation du chemin sélectionné", commandsList)) |
952 | | commandsList = [] |
953 | | # Après la séparation, trouvez le segment qui relie à nouveau le carrefour giratoire |
954 | | for waypartconnectedtoroundabout in adjacent_node_to_split_on.getParentWays(): |
955 | | if common_node in waypartconnectedtoroundabout.getNodes(): |
956 | | break |
957 | | if len(way.getNodes())==2: |
958 | | if common_node == waypartconnectedtoroundabout.firstNode(): |
959 | | adjacent_node_to_split_on = waypartconnectedtoroundabout.lastNode() |
960 | | else: |
961 | | adjacent_node_to_split_on = waypartconnectedtoroundabout.firstNode() |
962 | | # Il est temps de faire quelque chose |
963 | | # de faire une copie du chemin |
964 | | modified_way = Way(waypartconnectedtoroundabout) |
965 | | # remplacer ses nœuds, de sorte qu'il se transforme en une fourche |
966 | | modified_way.setNodes([node_before, adjacent_node_to_split_on, node_after]) |
967 | | # ajouter un attribut "oneway" |
968 | | modified_way.put('oneway', 'yes') |
969 | | # appliquer les modifications |
970 | | commandsList.append(Command.ChangeCommand( |
971 | | waypartconnectedtoroundabout, modified_way)) |
972 | | MainApplication.undoRedo.add(Command.SequenceCommand( |
973 | | "Ajouter un attribut oneway et créer un chemin en forme de fourche", commandsList)) |
974 | | commandsList = [] |
975 | | # séparer ce chemin là où il se divise en fourche |
976 | | commandsList.append(Command.SplitWayCommand.split( |
977 | | waypartconnectedtoroundabout, [adjacent_node_to_split_on], [])) |
978 | | MainApplication.undoRedo.add(Command.SequenceCommand( |
979 | | "Divisez la fourche en deux chemins", commandsList)) |
980 | | commandsList = [] |
981 | | |
982 | | }}} |
983 | | |
984 | | |
985 | | == Convertir un chemin qui se raccorde à un carrefour giratoire fractionné en une bifurcation == |
986 | | Ce script fait la même chose que le précédent, mais il fonctionne aussi sur les carrefours giratoires dont les chemins sont séparés. Il ne s'occupe pas des relations. |
987 | | |
988 | | {{{#!python |
989 | | from javax.swing import JOptionPane |
990 | | from org.openstreetmap.josm.gui import MainApplication |
991 | | |
992 | | import org.openstreetmap.josm.command as Command |
993 | | import org.openstreetmap.josm.data.osm.Way as Way |
994 | | |
995 | | editLayer = MainApplication.getLayerManager().getEditLayer() |
996 | | print '==== Fresh run ====' |
997 | | if editLayer and editLayer.data: |
998 | | selected_ways = editLayer.data.getSelectedWays() |
999 | | print selected_ways |
1000 | | |
1001 | | if not(selected_ways) or len(selected_ways)>1: |
1002 | | JOptionPane.showMessageDialog(MainApplication.parent, "Veuillez sélectionner un chemin unique qui se connecte à un carrefour giratoire") |
1003 | | else: |
1004 | | for way in selected_ways: |
1005 | | if way.get('oneway') in ['yes', '-1']: |
1006 | | JOptionPane.showMessageDialog(MainApplication.parent, "Ce chemin est balisé par un attribut oneway") |
1007 | | elif way.get('junction') in ['roundabout']: |
1008 | | JOptionPane.showMessageDialog(MainApplication.parent, "Ce chemin fait partie d'un carrefour giratoire") |
1009 | | else: |
1010 | | node_before = None |
1011 | | node_after = None |
1012 | | common_node = None |
1013 | | common_node_becomes_node_before=None |
1014 | | roundabout_way_before = None |
1015 | | roundabout_way_after = None |
1016 | | for fln in [way.firstNode(), way.lastNode()]: |
1017 | | print 'fln', fln |
1018 | | for parentway in fln.getParentWays(): |
1019 | | if parentway.get("junction")=='roundabout': |
1020 | | print parentway.get('name') |
1021 | | if parentway.isClosed(): |
1022 | | # carrefour giratoire non fractionné |
1023 | | for n in parentway.getNodes(): |
1024 | | if common_node: |
1025 | | # noeud commun trouvé entre le chemin sélectionné |
1026 | | # et le carrefour giratoire dans l'itération précédente |
1027 | | node_after = n |
1028 | | print node_before.get('name') |
1029 | | print node_after.get('name') |
1030 | | # nous avons terminé ici |
1031 | | break |
1032 | | if n.getId() == fln.getId(): |
1033 | | # c'est le nœud que le carrefour giratoire a en commun avec le chemin sélectionné |
1034 | | common_node = n |
1035 | | print common_node.get('name') |
1036 | | if not(node_before): |
1037 | | # normalement, nous avons rencontré un nœud du carrefour giratoire bien |
1038 | | # avant celui-ci, mais si le nœud commun est le premier nœud |
1039 | | # d'un carrefour giratoire non divisé, nous devrons prendre le dernier |
1040 | | # nœud du carrefour giratoire à la place |
1041 | | node_before = parentway.getNodes()[-2] |
1042 | | node_after = parentway.getNodes()[1] |
1043 | | print node_before.get('name') |
1044 | | print node_after.get('name') |
1045 | | break |
1046 | | # si ce n'est pas le cas, nous passons par la boucle une fois de plus pour mettre le prochain |
1047 | | # noeud dans node_after |
1048 | | continue |
1049 | | node_before = n |
1050 | | else: |
1051 | | # il s'agit d'un carrefour giratoire fractionné |
1052 | | if parentway.firstNode().getId() == fln.getId(): |
1053 | | node_after = parentway.getNodes()[1] |
1054 | | roundabout_way_after = parentway |
1055 | | print 'node after', node_after.get('name') |
1056 | | else: |
1057 | | node_before = parentway.getNodes()[-2] |
1058 | | roundabout_way_before = parentway |
1059 | | if node_before and node_after: |
1060 | | common_node = fln |
1061 | | break |
1062 | | |
1063 | | if common_node: |
1064 | | # si common_node est déjà défini à ce stade, cela signifie que c'était |
1065 | | # le premier noeud du chemin sélectionné, |
1066 | | # il devra donc être remplacé par node_before dans le chemin sélectionné |
1067 | | common_node_becomes_node_before = True |
1068 | | break |
1069 | | else: |
1070 | | common_node_becomes_node_before = False |
1071 | | |
1072 | | if common_node.getId() == way.firstNode().getId(): |
1073 | | adjacent_node_to_split_on = way.getNodes()[1] |
1074 | | else: |
1075 | | adjacent_node_to_split_on = way.getNodes()[-1] |
1076 | | |
1077 | | if not(common_node) or ((parentway.isClosed() and common_node_becomes_node_before==None)): |
1078 | | JOptionPane.showMessageDialog(MainApplication.parent, "Please select a way that connects to a roundabout") |
1079 | | else: |
1080 | | commandsList = [] |
1081 | | if len(way.getNodes())>2: |
1082 | | # séparer le dernier segment avant le carrefour giratoire si nécessaire |
1083 | | commandsList.append(Command.SplitWayCommand.split( |
1084 | | way, [adjacent_node_to_split_on], [])) |
1085 | | MainApplication.undoRedo.add(Command.SequenceCommand( |
1086 | | "Partager le chemin sélectionné", commandsList)) |
1087 | | commandsList = [] |
1088 | | # Après la séparation, trouvez le segment qui rejoint à nouveau le carrefour giratoire |
1089 | | for waypartconnectedtoroundabout in adjacent_node_to_split_on.getParentWays(): |
1090 | | if waypartconnectedtoroundabout.get('junction') == 'roundabout': |
1091 | | continue |
1092 | | if common_node in waypartconnectedtoroundabout.getNodes(): |
1093 | | break |
1094 | | if len(way.getNodes())==2: |
1095 | | if common_node == waypartconnectedtoroundabout.firstNode(): |
1096 | | adjacent_node_to_split_on = waypartconnectedtoroundabout.lastNode() |
1097 | | else: |
1098 | | adjacent_node_to_split_on = waypartconnectedtoroundabout.firstNode() |
1099 | | # Il est temps de faire quelque chose |
1100 | | # faire une copie du chemin |
1101 | | modified_way = Way(waypartconnectedtoroundabout) |
1102 | | # replacer ses nœuds, il devient donc une fourche |
1103 | | modified_way.setNodes([node_before, adjacent_node_to_split_on, node_after]) |
1104 | | # ajouter un attribut oneway |
1105 | | modified_way.put('oneway', 'yes') |
1106 | | # appliquer les modifications |
1107 | | commandsList.append(Command.ChangeCommand( |
1108 | | waypartconnectedtoroundabout, modified_way)) |
1109 | | MainApplication.undoRedo.add(Command.SequenceCommand( |
1110 | | "Ajouter l'attribut oneway et créer un chemin en forme de fourche", commandsList)) |
1111 | | commandsList = [] |
1112 | | # partager ce chemin où il y a une fourche |
1113 | | commandsList.append(Command.SplitWayCommand.split( |
1114 | | waypartconnectedtoroundabout, [adjacent_node_to_split_on], [])) |
1115 | | MainApplication.undoRedo.add(Command.SequenceCommand( |
1116 | | "Divisez la fourche en deux chemins", commandsList)) |
1117 | | commandsList = [] |
1118 | | |
1119 | | if roundabout_way_before and roundabout_way_after: |
1120 | | origway = roundabout_way_before |
1121 | | roundabout_way_before.addNode(node_after) |
1122 | | commandsList.append(Command.ChangeCommand( |
1123 | | origway, roundabout_way_before)) |
1124 | | origway = roundabout_way_after |
1125 | | roundabout_way_after.removeNode(common_node) |
1126 | | commandsList.append(Command.ChangeCommand( |
1127 | | origway,roundabout_way_after)) |
1128 | | # middleway = Way(roundabout_way_after).setNodes( |
1129 | | # [node_before, common_node, node_after]) |
1130 | | # commandsList.append(Command.AddCommand(editLayer.data, middleway)) |
1131 | | # MainApplication.undoRedo.add(Command.SequenceCommand( |
1132 | | # "Ajouter un chemin sur le carrefour giratoire où la fourche s'attache", commandsList)) |
1133 | | commandsList.append(Command.SplitWayCommand.split( |
1134 | | roundabout_way_before, [node_before], [])) |
1135 | | MainApplication.undoRedo.add(Command.SequenceCommand( |
1136 | | "decoupe le chemin du giratoire", commandsList)) |
1137 | | commandsList = [] |
1138 | | |
1139 | | }}} |
1140 | | |
1141 | | |
1142 | | == Exporter une collection d'itinéraires vers un fichier GPX Garmin == |
1143 | | Exporter une collection d'itinéraires vers un fichier GPX Garmin (pas un super exemple, car je ne pense pas que nous fassions encore des relations de collection) : |
1144 | | |
1145 | | {{{#!python |
1146 | | #!/bin/jython |
1147 | | ''' |
1148 | | RWN2Garmin.py - Convertisseur de réseaux numérotés en fichiers GPX Garmin |
1149 | | Ce code est publié sous la licence GNU General Public License v2 ou ultérieure. |
1150 | | |
1151 | | La GPL v3 est disponible ici : |
1152 | | https://www.gnu.org/licenses/gpl.html |
1153 | | |
1154 | | Il est livré sans aucune garantie. |
1155 | | ''' |
1156 | | from javax.swing import JOptionPane, JDialog |
1157 | | from java.awt.event import ActionListener, ActionEvent |
1158 | | from org.openstreetmap.josm import Main |
1159 | | import org.openstreetmap.josm.command as Command |
1160 | | import org.openstreetmap.josm.data.osm.Node as Node |
1161 | | import org.openstreetmap.josm.data.osm.Way as Way |
1162 | | import org.openstreetmap.josm.data.osm.TagCollection as TagCollection |
1163 | | import org.openstreetmap.josm.data.osm.DataSet as DataSet |
1164 | | import time |
1165 | | |
1166 | | f = open('C:/export.gpx', 'w') |
1167 | | f.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n') |
1168 | | f.write('<gpx xmlns="https://www.topografix.com/GPX/1/1" creator="OSM Route Manager" version="1.1" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://wwws.topografix.com/GPX/1/1 https://wwws.topografix.com/GPX/1/1/gpx.xsd">\n') |
1169 | | f.write('<!-- All data by OpenStreetMap, licensed under cc-by-sa-2.0 (https://creativecommons.org/licenses/by-sa/2.0/). -->\n') |
1170 | | |
1171 | | editLayer = Main.getLayerManager().getEditLayer() |
1172 | | if editLayer and editLayer.data: |
1173 | | selectedRelations = mv.editLayer.data.getSelectedRelations() |
1174 | | |
1175 | | if not(selectedRelations): |
1176 | | JOptionPane.showMessageDialog(Main.parent, "Veuillez sélectionner une relation de collection") |
1177 | | else: |
1178 | | print |
1179 | | for collection in selectedRelations: |
1180 | | print 'COLLECTION:', collection |
1181 | | for member in collection.getMembers(): |
1182 | | print 'MEMBER:',member |
1183 | | if member.isNode(): |
1184 | | node = member.getNode() |
1185 | | coords = node.getCoor() |
1186 | | lon = coords.getX() |
1187 | | lat = coords.getY() |
1188 | | rwn_ref = node.get('rwn_ref') |
1189 | | f.write('\t<wpt lat="' + str(lat) + '" lon="' + str(lon) + '">\n') |
1190 | | if rwn_ref: |
1191 | | f.write('\t\t<name>' + rwn_ref + '</name>\n') |
1192 | | f.write('\t</wpt>\n') |
1193 | | for member in collection.getMembers(): |
1194 | | if member.isRelation(): |
1195 | | routerelation = member.getRelation() |
1196 | | f.write('\t<trk>\n') |
1197 | | networkname = routerelation.get('network:name') |
1198 | | if not(networkname): |
1199 | | networkname = '' |
1200 | | else: |
1201 | | networkname += ' ' |
1202 | | note = routerelation.get('note') |
1203 | | if not(note): note = '' |
1204 | | f.write('\t\t<name>' + networkname + note + '</name>\n') |
1205 | | f.write('\t\t<src>OpenStreetMap.org</src>\n') |
1206 | | f.write('\t\t<type>foot</type>\n') |
1207 | | for routerelmember in routerelation.getMembers(): |
1208 | | if routerelmember.isWay(): |
1209 | | f.write('\t\t<trkseg>\n') |
1210 | | way=routerelmember.getWay() |
1211 | | for waynode in way.getNodes(): |
1212 | | coords = waynode.getCoor() |
1213 | | lon = coords.getX() |
1214 | | lat = coords.getY() |
1215 | | f.write('\t\t\t<trkpt lat="' + str(lat) + '" lon="' + str(lon) + '"> </trkpt>\n') |
1216 | | |
1217 | | f.write('\t\t</trkseg>\n') |
1218 | | f.write('\t</trk>\n') |
1219 | | f.write('</gpx>\n') |
1220 | | f.close() |
1221 | | }}} |
1222 | | |
1223 | | |
1224 | | == Télécharger les éléments parents manquants pour l'élément sélectionné == |
1225 | | |
1226 | | {{{#!python |
1227 | | #!/bin/jython |
1228 | | ''' |
1229 | | |
1230 | | Ce code est publié sous la licence |
1231 | | "GNU General Public" v2 ou ultérieure. |
1232 | | |
1233 | | La GPL v3 est disponible ici : |
1234 | | https://www.gnu.org/licenses/gpl.html |
1235 | | |
1236 | | La GPL v2 est disponible ici : |
1237 | | https://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
1238 | | |
1239 | | Il est livré sans aucune garantie. |
1240 | | |
1241 | | Ce code illustre comment utiliser Jython pour : |
1242 | | * Télécharger tous les référents d'un élément |
1243 | | |
1244 | | ''' |
1245 | | from javax.swing import JOptionPane |
1246 | | from org.openstreetmap.josm import Main |
1247 | | import org.openstreetmap.josm.actions.DownloadReferrersAction as DownloadReferrersAction |
1248 | | |
1249 | | editLayer = Main.getLayerManager().getEditLayer() |
1250 | | if editLayer and editLayer.data: |
1251 | | selectedElements = editLayer.data.getSelected() |
1252 | | |
1253 | | if not(selectedElements): |
1254 | | JOptionPane.showMessageDialog(Main.parent, "Veuillez sélectionner un élément") |
1255 | | else: |
1256 | | DownloadReferrersAction.downloadReferrers(editLayer, selectedElements) |
1257 | | }}} |
1258 | | |
1259 | | |
1260 | | == Télécharger les membres absents de la relation == |
1261 | | |
1262 | | {{{ |
1263 | | #!/bin/jython |
1264 | | ''' |
1265 | | |
1266 | | Ce code est publié sous la licence |
1267 | | "GNU General Public" v2 ou ultérieure. |
1268 | | |
1269 | | La GPL v3 est disponible ici : |
1270 | | https://www.gnu.org/licenses/gpl.html |
1271 | | |
1272 | | La GPL v2 est disponible ici : |
1273 | | https://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
1274 | | |
1275 | | Il est livré sans aucune garantie. |
1276 | | |
1277 | | Ce code illustre comment utiliser Jython pour : |
1278 | | * Télécharger tous les membres absents de la relation |
1279 | | |
1280 | | ''' |
1281 | | from javax.swing import JOptionPane |
1282 | | from org.openstreetmap.josm import Main |
1283 | | import org.openstreetmap.josm.data.osm.Node as Node |
1284 | | import org.openstreetmap.josm.data.osm.Way as Way |
1285 | | import org.openstreetmap.josm.data.osm.Relation as Relation |
1286 | | import org.openstreetmap.josm.data.osm.TagCollection as TagCollection |
1287 | | import org.openstreetmap.josm.data.osm.DataSet as DataSet |
1288 | | import org.openstreetmap.josm.data.osm.RelationMember as RelationMember |
1289 | | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask as DownloadRelationMemberTask |
1290 | | |
1291 | | editLayer = Main.getLayerManager().getEditLayer() |
1292 | | if editLayer: |
1293 | | selectedRelations = editLayer.data.getSelectedRelations() |
1294 | | |
1295 | | if not(selectedRelations): |
1296 | | JOptionPane.showMessageDialog(Main.parent, "Please select a relation") |
1297 | | else: |
1298 | | for relation in selectedRelations: |
1299 | | if relation.hasIncompleteMembers(): |
1300 | | #print dir(relation) |
1301 | | print dir(DownloadRelationMemberTask) |
1302 | | DownloadRelationMemberTask.run(DownloadRelationMemberTask(relation, relation.getIncompleteMembers(), editLayer )) |
1303 | | }}} |
1304 | | |
1305 | | |
1306 | | == Valider une relation de route rcn == |
1307 | | |
1308 | | {{{ |
1309 | | #!/bin/jython |
1310 | | ''' |
1311 | | - Validation d'une relation de route rcn |
1312 | | |
1313 | | Ce code est publié sous la licence |
1314 | | "GNU General Public" v2 ou ultérieure. |
1315 | | |
1316 | | La GPL v3 est disponible ici : |
1317 | | https://www.gnu.org/licenses/gpl.html |
1318 | | |
1319 | | La GPL v2 est disponible ici : |
1320 | | https://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
1321 | | |
1322 | | Il est livré sans aucune garantie. |
1323 | | |
1324 | | Ce code illustre comment utiliser Jython pour : |
1325 | | * passer en boucle tous les membres d'une relation de route |
1326 | | * déterminer si le membre est un noeud, un chemin ou une relation |
1327 | | * ajouter/changer les propriétés d'une relation |
1328 | | * Supprimer les propriétés d'une relation |
1329 | | * Ajouter des membres à une relation |
1330 | | * Supprimer des membres d'une relation |
1331 | | * trier tous les membres en arrière |
1332 | | |
1333 | | * Comment définir un élément sélectionné |
1334 | | |
1335 | | |
1336 | | ''' |
1337 | | from javax.swing import JOptionPane |
1338 | | from org.openstreetmap.josm import Main |
1339 | | import org.openstreetmap.josm.command as Command |
1340 | | import org.openstreetmap.josm.data.osm.Node as Node |
1341 | | import org.openstreetmap.josm.data.osm.Way as Way |
1342 | | import org.openstreetmap.josm.data.osm.Relation as Relation |
1343 | | import org.openstreetmap.josm.data.osm.TagCollection as TagCollection |
1344 | | import org.openstreetmap.josm.data.osm.DataSet as DataSet |
1345 | | import org.openstreetmap.josm.data.osm.RelationMember as RelationMember |
1346 | | import re |
1347 | | |
1348 | | commandsList = [] |
1349 | | reNumberDashNumber = re.compile(r'\d+-\d+') |
1350 | | def getMapView(): |
1351 | | if Main.main and Main.main.map: |
1352 | | return Main.main.map.mapView |
1353 | | else: |
1354 | | return None |
1355 | | |
1356 | | mv = getMapView() |
1357 | | if mv and mv.editLayer and mv.editLayer.data: |
1358 | | dummy_relation = Relation() |
1359 | | selectedRelations = mv.editLayer.data.getSelectedRelations() |
1360 | | |
1361 | | if not(selectedRelations): |
1362 | | JOptionPane.showMessageDialog(Main.parent, "Veuillez sélectionner une relation d'itinéraire") |
1363 | | else: |
1364 | | print |
1365 | | for route in selectedRelations: |
1366 | | newRelation = Relation(route) |
1367 | | relationChanged = False |
1368 | | name = route.get('name') |
1369 | | if name: |
1370 | | if reNumberDashNumber.match(name): |
1371 | | print 'en supprimant le nom lorsqu'il est de la forme ##-##' |
1372 | | newRelation.remove('name') |
1373 | | relationChanged = True |
1374 | | else: |
1375 | | name = '' |
1376 | | ref = route.get('ref') |
1377 | | if ref: |
1378 | | if reNumberDashNumber.match(ref): |
1379 | | print 'en supprimant ref lorsqu'elle est de la forme ##-##' |
1380 | | newRelation.remove('ref') |
1381 | | relationChanged = True |
1382 | | else: |
1383 | | ref = '' |
1384 | | if relationChanged: |
1385 | | commandsList.append(Command.ChangeCommand(route, newRelation)) |
1386 | | |
1387 | | Main.main.undoRedo.add(Command.SequenceCommand("Removing name and/or ref " + name + '/' + ref, commandsList)) |
1388 | | commandsList = [] |
1389 | | |
1390 | | rcn_refs = []; route_relation_names = []; memberslist = [] |
1391 | | endnodes = []; prev_endnodes = [] |
1392 | | continuous_forward = True; continuous_backward = True |
1393 | | prev_role = None; prev_endnodes_before_forward = None; last_endnodes_before_backward = None |
1394 | | for member in route.getMembers(): |
1395 | | if member.isWay(): |
1396 | | role = member.getRole() |
1397 | | memberslist.append(member) |
1398 | | way = member.getWay() |
1399 | | #JOptionPane.showMessageDialog(Main.parent, 'way is selected') |
1400 | | endnodes = [way.getNode(0), way.getNode(way.nodesCount-1)] |
1401 | | notfoundyet = True |
1402 | | for endnode in endnodes: |
1403 | | # inventorizing of rcn_ref on end nodes |
1404 | | rcn_ref = endnode.get('rcn_ref') |
1405 | | if rcn_ref: |
1406 | | rcn_refs.append(int(rcn_ref)) |
1407 | | for referrer in endnode.getReferrers(): |
1408 | | if referrer.getType() is dummy_relation.getType(): |
1409 | | if referrer.get('type')=='network' and referrer.get('network')=='rcn': |
1410 | | relname=referrer.get('name') |
1411 | | if relname: |
1412 | | route_relation_names.append(relname) |
1413 | | |
1414 | | elif referrer.get('type')=='collection': |
1415 | | route_relation_names.append('Node not assigned to network yet') |
1416 | | # checking for continuity on ways |
1417 | | if notfoundyet: |
1418 | | if role: |
1419 | | if prev_role: |
1420 | | if role=='forward' and prev_role=='forward' and endnode in prev_endnodes: |
1421 | | notfoundyet = False |
1422 | | elif role=='forward' and prev_role=='backward' and endnode in last_endnodes_before_backward: |
1423 | | notfoundyet = False |
1424 | | elif role=='backward' and prev_role=='forward' and endnode in prev_endnodes: |
1425 | | notfoundyet = False |
1426 | | elif role=='backward' and prev_role=='backward' and endnode in prev_endnodes: |
1427 | | notfoundyet = False |
1428 | | else: |
1429 | | if role=='forward' and endnode in prev_endnodes: |
1430 | | notfoundyet = False |
1431 | | elif role=='backward' and endnode in prev_endnodes: |
1432 | | notfoundyet = False |
1433 | | else: |
1434 | | if prev_role: |
1435 | | if prev_role=='forward' and endnode in prev_endnodes: |
1436 | | notfoundyet = False |
1437 | | elif prev_role=='backward' and endnode in last_endnodes_before_backward: |
1438 | | notfoundyet = False |
1439 | | else: |
1440 | | if endnode in prev_endnodes: |
1441 | | notfoundyet = False |
1442 | | # Analysis of continuity of ways |
1443 | | if prev_endnodes and notfoundyet: |
1444 | | if role: |
1445 | | if role == 'forward': |
1446 | | continuous_forward = False |
1447 | | elif role == 'backward': |
1448 | | continuous_backward = False |
1449 | | else: |
1450 | | continuous_forward = False |
1451 | | continuous_backward = False |
1452 | | if role=='forward': |
1453 | | if not(prev_endnodes_before_forward): |
1454 | | prev_endnodes_before_forward = prev_endnodes |
1455 | | elif prev_role=='forward' and role=='backward': |
1456 | | if not(last_endnodes_before_backward): |
1457 | | last_endnodes_before_backward = prev_endnodes |
1458 | | elif not(role) and prev_role=='backward': |
1459 | | prev_endnodes_before_forward = None |
1460 | | prev_role = role |
1461 | | prev_endnodes = endnodes |
1462 | | # Drawing conclusions about continuity of ways |
1463 | | if continuous_forward: |
1464 | | print 'Itinéraire continu dans la direction vers devant' |
1465 | | else: |
1466 | | print 'Itinéraire PAS CONTINU dans la direction vers devant' |
1467 | | if continuous_backward: |
1468 | | print 'Itinéraire continu dans la direction vers arrière' |
1469 | | else: |
1470 | | print 'Itinéraire PAS CONTINU dans la direction vers arrière' |
1471 | | |
1472 | | # Drawing conclusions about rcn_refs |
1473 | | print rcn_refs |
1474 | | if len(rcn_refs) > 1: |
1475 | | newRelation = Relation(route) |
1476 | | relationChanged = False |
1477 | | |
1478 | | if rcn_refs[0] > rcn_refs[1]: |
1479 | | rcn_refs.sort() |
1480 | | print 'Inversion de l'ordre des membres' |
1481 | | for member in reversed(memberslist): |
1482 | | newRelation.addMember( newRelation.getMembersCount(), member) |
1483 | | newRelation.removeMember (0) |
1484 | | commandsList.append(Command.ChangeCommand(route, newRelation)) |
1485 | | Main.main.undoRedo.add(Command.SequenceCommand("Flipping order of members", commandsList)) |
1486 | | commandsList = [] |
1487 | | note = route.get('note') |
1488 | | newNote = str(rcn_refs[0]).zfill(2) + '-' + str(rcn_refs[1]).zfill(2) |
1489 | | if not(note) or note != newNote: |
1490 | | if not(note): note = 'nothing' |
1491 | | newRelation.put('note', newNote) |
1492 | | relationChanged = True |
1493 | | commandsList.append(Command.ChangeCommand(route, newRelation)) |
1494 | | |
1495 | | Main.main.undoRedo.add(Command.SequenceCommand("Changing note from " + note + ' to ' + newNote, commandsList)) |
1496 | | commandsList = [] |
1497 | | |
1498 | | if len(route_relation_names) > 1 and route_relation_names[0] != route_relation_names[1]: |
1499 | | print |
1500 | | print 'Il s'agit probablement d'une CONNEXION à un autre réseau' |
1501 | | print route_relation_names |
1502 | | else: |
1503 | | print 'moins de 2 noeuds terminaux avec rcn_ref trouvés' |
1504 | | }}} |
1505 | | |
1506 | | [wiki:/Help/Plugin/Scripting/Python/RCN_Route_Validator] |
1507 | | |
1508 | | |
1509 | | == Suppression des chemins supplémentaires résultant d'opérations Potlatch == |
1510 | | Suppression des chemins supplémentaires résultant des opérations de division des chemins de Potlatch dans le cadre des relations d'interdiction de tourner. |
1511 | | |
1512 | | {{{#!python |
1513 | | #!/bin/jython |
1514 | | |
1515 | | ''' |
1516 | | RepairTurns.py - Suppression des chemins supplémentaires résultant des opérations de division des chemins de Potlatch dans le cadre des relations d'interdiction de tourner |
1517 | | |
1518 | | Ce code est publié sous la licence |
1519 | | "GNU General Public" v2 ou ultérieure. |
1520 | | |
1521 | | La GPL v3 est disponible ici : |
1522 | | https://www.gnu.org/licenses/gpl.html |
1523 | | |
1524 | | Il est livré sans aucune garantie. |
1525 | | |
1526 | | Ce code parcourt en boucle les relations d'interdiction de tourner sélectionnées, en essayant de supprimer les chemins divisés à l'origine de la même manière (avec des rôles to / from) sous les relations dinterdiction de tourner qui ne devraient plus rester membres de ces relations, à la suite d'un problème de Potlatch : https://trac.openstreetmap.org/ticket/3254. |
1527 | | |
1528 | | Ne fonctionne que pour les interdiction de tourner avec un noeud via |
1529 | | |
1530 | | e.g. Original : from: Way1, via: Node, to:Way2 |
1531 | | Split : from: Way1a, from: Way1b, via: Node, to: Way2 |
1532 | | After running : from: Way1b, via: Node, to: Way2 |
1533 | | |
1534 | | Ce code illustre comment utiliser Jython pour : |
1535 | | * traiter les éléments sélectionnés |
1536 | | * télécharger les primitives manquantes dans le même thread (blocage) |
1537 | | * traiter, valider et supprimer des membres de relations |
1538 | | |
1539 | | ''' |
1540 | | from javax.swing import JOptionPane |
1541 | | from org.openstreetmap.josm import Main |
1542 | | import org.openstreetmap.josm.data.osm.Node as Node |
1543 | | import org.openstreetmap.josm.data.osm.Way as Way |
1544 | | import org.openstreetmap.josm.data.osm.Relation as Relation |
1545 | | import org.openstreetmap.josm.data.osm.TagCollection as TagCollection |
1546 | | import org.openstreetmap.josm.data.osm.DataSet as DataSet |
1547 | | import org.openstreetmap.josm.data.osm.RelationMember as RelationMember |
1548 | | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask as DownloadRelationMemberTask |
1549 | | import org.openstreetmap.josm.io.MultiFetchServerObjectReader as MultiFetchServerObjectReader |
1550 | | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor as PleaseWaitProgressMonitor |
1551 | | import org.openstreetmap.josm.command as Command |
1552 | | |
1553 | | class RestrictionError(Exception): |
1554 | | pass |
1555 | | |
1556 | | def getMembers (restriction): |
1557 | | memberlist = dict() |
1558 | | for member in restriction.getMembers(): |
1559 | | memberRole = member.getRole() |
1560 | | if memberRole == "via": |
1561 | | if member.isNode() and not "via" in memberlist: |
1562 | | memberlist[memberRole] = [member] |
1563 | | else: |
1564 | | raise RestrictionError, "Plus d'un avec rôle via ou avec via mais ce n'est pas un nœud" |
1565 | | elif memberRole in ("from", "to"): |
1566 | | if member.isWay(): |
1567 | | try: |
1568 | | memberlist[memberRole].append (member) |
1569 | | except KeyError: |
1570 | | memberlist[memberRole] = [member] |
1571 | | else: |
1572 | | raise RestrictionError, "avec le rôle From ou to mais n'est pas un chemin" |
1573 | | else: |
1574 | | raise RestrictionError, "Rôle inconnu " + memberRole |
1575 | | |
1576 | | if len(memberlist.keys())<3: |
1577 | | raise RestrictionError, "Certains rôles manquent : Seulement " + ",".join (memberlist.keys()) + " found" |
1578 | | return memberlist |
1579 | | |
1580 | | def downloadPrimitives (primitives, editlayer): |
1581 | | """ |
1582 | | Télécharger une liste de primitives depuis le serveur, et les fusionner dans editlayer. |
1583 | | Blocking. |
1584 | | """ |
1585 | | monitor = PleaseWaitProgressMonitor() |
1586 | | monitor.showForegroundDialog() |
1587 | | |
1588 | | print "Téléchargement" |
1589 | | try: |
1590 | | objectReader = MultiFetchServerObjectReader().append (primitives) |
1591 | | dataSet = objectReader.parseOsm (monitor) |
1592 | | editlayer.mergeFrom (dataSet) |
1593 | | editlayer.onPostDownloadFromServer() |
1594 | | if (not objectReader.getMissingPrimitives().isEmpty()) : |
1595 | | raise RestrictionError, "Impossible de télécharger les primitives manquantes" |
1596 | | print "Téléchargement terminé" |
1597 | | finally: |
1598 | | monitor.close() |
1599 | | |
1600 | | def checkIfConnected (node, way, reverse): |
1601 | | """ |
1602 | | Return (connecté, prochain nœud à comparer (c'est-à-dire l'autre extrémité du nœud) si vrai) |
1603 | | """ |
1604 | | if (way.isOneway() != 0 and reverse): |
1605 | | return node == way.lastNode(True), way.firstNode(True) # True: auto process case whan isOneway == -1 |
1606 | | if (way.isOneway() != 0): # pas d'inversion |
1607 | | return node == way.firstNode(True), way.lastNode(True) |
1608 | | if node == way.firstNode(): |
1609 | | return True, way.lastNode() |
1610 | | if node == way.lastNode(): |
1611 | | return True, way.firstNode() |
1612 | | return False, node |
1613 | | |
1614 | | |
1615 | | def repairrestriction (relation, memberlist, editLayer): |
1616 | | """ |
1617 | | Télécharger les membres manquants si nécessaire, obtenir la liste des membres à supprimer, et les supprimer si aucune erreur n'a été soulevée pendant la vérification |
1618 | | """ |
1619 | | incompleteMembers = relation.getIncompleteMembers() |
1620 | | if incompleteMembers: |
1621 | | downloadPrimitives (incompleteMembers, editLayer); |
1622 | | |
1623 | | fromRemovalList = []; toRemovalList = [] |
1624 | | |
1625 | | if len (memberlist["from"]) >= 1: |
1626 | | currnode = memberlist["via"][0].getNode() |
1627 | | firstMember = True |
1628 | | failed = False; |
1629 | | # trace "from" des membres pour confirmer s'ils sont séparés d'un seul chemin |
1630 | | for member in reversed(memberlist['from']): |
1631 | | connected, currnode = checkIfConnected (currnode, member.getWay(), True) |
1632 | | if not connected: |
1633 | | if not firstMember: |
1634 | | raise RestrictionError, "des chemins non continus du noeud via" |
1635 | | failed = True |
1636 | | break |
1637 | | if not firstMember: |
1638 | | fromRemovalList.append (member) |
1639 | | else: |
1640 | | firstMember = False |
1641 | | |
1642 | | if failed: # Deuxième tentative en cas d'inversion de la séquence lors du fractionnement |
1643 | | currnode = memberlist["via"][0].getNode() |
1644 | | for member in memberlist['from']: |
1645 | | connected, currnode = checkIfConnected (currnode, member.getWay(), True) |
1646 | | if not connected: |
1647 | | raise RestrictionError, "des chemins non continus du noeud via" |
1648 | | if not firstMember: |
1649 | | fromRemovalList.append (member) |
1650 | | else: |
1651 | | firstMember = False |
1652 | | |
1653 | | if len (memberlist["to"]) >= 1: |
1654 | | currnode = memberlist["via"][0].getNode() |
1655 | | firstMember = True |
1656 | | failed = False |
1657 | | # trace les membres " to " pour confirmer s'ils ont été séparés d'un chemin |
1658 | | for member in memberlist['to']: |
1659 | | connected, currnode = checkIfConnected (currnode, member.getWay(), False) |
1660 | | if not connected: |
1661 | | if not firstMember: |
1662 | | raise RestrictionError, "chemins to non continus avec le noeud via" |
1663 | | failed = True |
1664 | | break |
1665 | | if not firstMember: |
1666 | | toRemovalList.append (member) |
1667 | | else: |
1668 | | firstMember = False |
1669 | | |
1670 | | if failed: # Deuxième tentative en cas d'inversion de la séquence lors du fractionnement |
1671 | | currnode = memberlist["via"][0].getNode() |
1672 | | for member in reversed(memberlist['to']): |
1673 | | connected, currnode = checkIfConnected (currnode, member.getWay(), False) |
1674 | | if not connected: |
1675 | | raise RestrictionError, "chemins to non continus avec le noeud via" |
1676 | | if not firstMember: |
1677 | | toRemovalList.append (member) |
1678 | | else: |
1679 | | firstMember = False |
1680 | | |
1681 | | # pour supprimer les chemins dans fromRemovalList, toRemovalList |
1682 | | newRelation = Relation(relation) |
1683 | | waysToRemove = set() |
1684 | | for removalList in [fromRemovalList, toRemovalList]: |
1685 | | waysToRemove |= set ([m.getWay() for m in removalList]) |
1686 | | newRelation.removeMembersFor (waysToRemove) |
1687 | | print "Supprimer le(s) chemin(s) identifiant : " + ",".join ([str(w.getId()) for w in waysToRemove]) |
1688 | | return Command.ChangeCommand (relation, newRelation) |
1689 | | |
1690 | | |
1691 | | validrestrictiontypes = ('only_straight_on', 'only_right_turn', 'only_left_turn', 'no_right_turn', 'no_left_turn', 'no_straight_on', 'no_u_turn') |
1692 | | editLayer = Main.getLayerManager().getEditLayer() |
1693 | | if editLayer and editLayer.data: |
1694 | | selectedRelations = editLayer.data.getSelectedRelations() |
1695 | | |
1696 | | if not(selectedRelations): |
1697 | | JOptionPane.showMessageDialog(Main.parent, "Veuillez sélectionner une ou plusieurs relations") |
1698 | | else: |
1699 | | commandsList = [] |
1700 | | for relation in selectedRelations: |
1701 | | if relation.get('type') == "restriction" and relation.get('restriction') in validrestrictiontypes: |
1702 | | try: |
1703 | | memberlist = getMembers (relation) |
1704 | | if (len (memberlist["from"])) > 1 or (len (memberlist["to"])) > 1 : |
1705 | | # Tentative de réparation |
1706 | | print "Tentative de réparation", |
1707 | | print "relation id: " + str(relation.getId()) |
1708 | | command = repairrestriction (relation, memberlist, mv.editLayer) |
1709 | | print "Succès" |
1710 | | commandsList.append (command) |
1711 | | except RestrictionError, e: |
1712 | | print str(e), "; relation id: "+ str(relation.getId()) |
1713 | | |
1714 | | |
1715 | | Main.main.undoRedo.add(Command.SequenceCommand("Réparation des interdictions de tourner", commandsList)) |
1716 | | commandsList=[] |
1717 | | }}} |
1718 | | |
1719 | | |
1720 | | == Voir également == |
1721 | | * La version originale de cette page en [wiki:/Help/Plugin/Scripting/Python anglais] |
1722 | | |
1723 | | |
1724 | | ---- |
1725 | | Retour au [wikitr:/Help/Plugin/Scripting Greffon Scripting] \\ |
1726 | | Retour à l'[wikitr:/Plugins Aide des Greffons] \\ |