Changeset 23189 in osm for applications/editors/josm/plugins/multipoly
- Timestamp:
- 2010-09-15T18:53:09+02:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/multipoly/src/multipoly
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/multipoly/src/multipoly/MultipolyAction.java
r19456 r23189 30 30 /** 31 31 * Create multipolygon from selected ways automatically. 32 * 32 * 33 33 * New relation with type=multipolygon is created 34 * 34 * 35 35 * If one or more of ways is already in relation with type=multipolygon or the 36 36 * way is not closed, then error is reported and no relation is created 37 * 37 * 38 38 * The "inner" and "outer" roles are guessed automatically. First, bbox is 39 39 * calculated for each way. then the largest area is assumed to be outside and … … 45 45 public class MultipolyAction extends JosmAction { 46 46 47 48 49 50 51 52 53 47 public MultipolyAction() { 48 super(tr("Create multipolygon"), "multipoly_create", 49 tr("Create multipolygon."), Shortcut.registerShortcut( 50 "tools:multipoly", tr("Tool: {0}", 51 tr("Create multipolygon")), KeyEvent.VK_M, 52 Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 53 } 54 54 55 56 57 * 58 59 60 61 55 /** 56 * The action button has been clicked 57 * 58 * @param e 59 * Action Event 60 */ 61 public void actionPerformed(ActionEvent e) { 62 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 63 // Get all ways in some type=multipolygon relation 64 HashSet<OsmPrimitive> relationsInMulti = new HashSet<OsmPrimitive>(); 65 for (Relation r : Main.main.getCurrentDataSet().getRelations()) { 66 if (!r.isUsable()) 67 continue; 68 if (r.get("type") != "multipolygon") 69 continue; 70 for (RelationMember rm : r.getMembers()) { 71 OsmPrimitive m = rm.getMember(); 72 if (m instanceof Way) { 73 relationsInMulti.add(m); 74 } 75 } 76 } 77 77 78 79 80 81 82 83 78 // List of selected ways 79 List<Way> selectedWays = new ArrayList<Way>(); 80 // Area of largest way (in square degrees) 81 double maxarea = 0; 82 // Which way is the largest one (outer) 83 Way maxWay = null; 84 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 85 // For every selected way 86 for (OsmPrimitive osm : Main.main.getCurrentDataSet().getSelected()) { 87 if (osm instanceof Way) { 88 Way way = (Way) osm; 89 // Check if way is already in another multipolygon 90 if (relationsInMulti.contains(osm)) { 91 JOptionPane 92 .showMessageDialog( 93 Main.parent, 94 tr("One of the selected ways is already part of another multipolygon.")); 95 return; 96 } 97 EastNorth first = null, last = null; 98 // Boundingbox of way 99 double minx = 9999, miny = 9999, maxx = -9999, maxy = -9999; 100 for (Pair<Node, Node> seg : way.getNodePairs(false)) { 101 if (first == null) 102 first = seg.a.getEastNorth(); 103 last = seg.b.getEastNorth(); 104 double x = seg.a.getEastNorth().east(); 105 double y = seg.a.getEastNorth().north(); 106 if (x < minx) 107 minx = x; 108 if (y < miny) 109 miny = y; 110 if (x > maxx) 111 maxx = x; 112 if (y > maxy) 113 maxy = y; 114 } 115 // Check if first and last node are the same 116 if (!first.equals(last)) { 117 JOptionPane 118 .showMessageDialog( 119 Main.parent, 120 tr("Multipolygon must consist only of closed ways.")); 121 return; 122 } 123 // Determine area 124 double area = (maxx - minx) * (maxy - miny); 125 selectedWays.add(way); 126 if (area > maxarea) { 127 maxarea = area; 128 maxWay = way; 129 } 130 } 131 } 132 132 133 134 135 136 133 if (Main.map == null) { 134 JOptionPane.showMessageDialog(Main.parent, tr("No data loaded.")); 135 return; 136 } 137 137 138 139 140 141 142 138 if (selectedWays.size() < 2) { 139 JOptionPane.showMessageDialog(Main.parent, 140 tr("You must select at least two ways.")); 141 return; 142 } 143 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 144 Collection<Command> cmds = new LinkedList<Command>(); 145 // Create new relation 146 Relation rel = new Relation(); 147 rel.put("type", "multipolygon"); 148 // Add ways to it 149 for (int i = 0; i < selectedWays.size(); i++) { 150 Way s = selectedWays.get(i); 151 String xrole = "inner"; 152 if (s == maxWay) 153 xrole = "outer"; 154 RelationMember rm = new RelationMember(xrole, s); 155 rel.addMember(rm); 156 } 157 // Add relation 158 cmds.add(new AddCommand(rel)); 159 // Commit 160 Main.main.undoRedo.add(new SequenceCommand(tr("Create multipolygon"), 161 cmds)); 162 Main.map.repaint(); 163 } 164 164 165 166 167 168 169 170 171 172 173 165 /** Enable this action only if something is selected */ 166 @Override 167 protected void updateEnabledState() { 168 if (getCurrentDataSet() == null) { 169 setEnabled(false); 170 } else { 171 updateEnabledState(getCurrentDataSet().getSelected()); 172 } 173 } 174 174 175 176 177 178 179 180 175 /** Enable this action only if something is selected */ 176 @Override 177 protected void updateEnabledState( 178 Collection<? extends OsmPrimitive> selection) { 179 setEnabled(selection != null && !selection.isEmpty()); 180 } 181 181 } -
applications/editors/josm/plugins/multipoly/src/multipoly/MultipolyPlugin.java
r19456 r23189 15 15 public class MultipolyPlugin extends Plugin { 16 16 17 17 protected String name; 18 18 19 20 21 22 23 24 25 26 27 28 29 19 public MultipolyPlugin(PluginInformation info) { 20 super(info); 21 name = tr("Create multipolygon"); 22 JMenu toolsMenu = null; 23 for (int i = 0; i < Main.main.menu.getMenuCount() && toolsMenu == null; i++) { 24 JMenu menu = Main.main.menu.getMenu(i); 25 String name = menu.getText(); 26 if (name != null && name.equals(tr("Tools"))) { 27 toolsMenu = menu; 28 } 29 } 30 30 31 32 33 34 35 36 37 38 39 31 if (toolsMenu == null) { 32 toolsMenu = new JMenu(name); 33 toolsMenu.add(new JMenuItem(new MultipolyAction())); 34 Main.main.menu.add(toolsMenu, 2); 35 } else { 36 toolsMenu.addSeparator(); 37 toolsMenu.add(new JMenuItem(new MultipolyAction())); 38 } 39 } 40 40 }
Note:
See TracChangeset
for help on using the changeset viewer.