Ticket #6386: IdNumber.patch

File IdNumber.patch, 4.8 KB (added by brycenesbitt, 2 years ago)

Patch: new download option "area around given node ID"

  • org/openstreetmap/josm/gui/download/DownloadDialog.java

     
    108108        downloadSelections.add(new BoundingBoxSelection()); 
    109109        downloadSelections.add(new PlaceSelection()); 
    110110        downloadSelections.add(new TileSelection()); 
     111        downloadSelections.add(new IdNumberSelection()); 
    111112 
    112113        // add selections from plugins 
    113114        PluginHandler.addDownloadSelection(downloadSelections); 
  • org/openstreetmap/josm/gui/download/IdNumberSelection.java

     
     1// License: GPL. See LICENSE file for details. 
     2package org.openstreetmap.josm.gui.download; 
     3 
     4import static org.openstreetmap.josm.tools.I18n.tr; 
     5 
     6import java.awt.GridBagLayout; 
     7import java.awt.event.ActionEvent; 
     8import java.awt.event.ActionListener; 
     9import java.awt.event.FocusAdapter; 
     10import java.awt.event.FocusEvent; 
     11import java.io.IOException; 
     12import java.util.logging.Logger; 
     13 
     14import javax.swing.JLabel; 
     15import javax.swing.JPanel; 
     16import javax.swing.JTextField; 
     17import javax.xml.parsers.DocumentBuilder; 
     18import javax.xml.parsers.DocumentBuilderFactory; 
     19import javax.xml.parsers.ParserConfigurationException; 
     20 
     21import org.openstreetmap.josm.data.Bounds; 
     22import org.openstreetmap.josm.tools.GBC; 
     23import org.w3c.dom.Document; 
     24import org.w3c.dom.NamedNodeMap; 
     25import org.w3c.dom.Node; 
     26import org.xml.sax.SAXException; 
     27/** 
     28 * ID Number Selector 
     29 *  
     30 * Input method for the "Download from OSM" dialog box.  Given an OSM element ID number, 
     31 * it finds the center point and loads small additional area on each side. 
     32 * Very handy if you've found a problem element in an XML or database dump. 
     33 *  
     34 * Example: enter node id 1, to see the first node ever added to OSM 
     35 * TODO: support ways/relations 
     36 * 
     37 * @author Bryce Nesbitt, April 2011 
     38 * 
     39 */ 
     40public class IdNumberSelection implements DownloadSelection { 
     41        private static final Logger logger = Logger.getLogger(IdNumberSelection.class.getName()); 
     42 
     43    private DownloadDialog parent; 
     44    private JTextField osmid = new JTextField(11); 
     45    private int lastid = 0; 
     46 
     47    public void addGui(final DownloadDialog gui) { 
     48        BoundingBoxBuilder bboxbuilder = new BoundingBoxBuilder(); 
     49        osmid.addFocusListener (bboxbuilder); 
     50        osmid.addActionListener(bboxbuilder); 
     51         
     52        final JPanel dlg = new JPanel(new GridBagLayout()); 
     53        dlg.add(new JLabel(tr("OSM Node ID")), GBC.std().insets(10,20,5,0)); 
     54        dlg.add(osmid, GBC.std().insets(0,20,0,0)); 
     55        gui.addDownloadAreaSelector(dlg, tr("Node ID")); 
     56        parent = gui; 
     57    } 
     58 
     59    public void setDownloadArea(Bounds area) { 
     60    } 
     61     
     62    class BoundingBoxBuilder extends FocusAdapter implements ActionListener { 
     63  
     64        protected void refreshBounds() { 
     65            double fudgelon=.001, fudgelat=.001; 
     66            double mylon=0,       mylat=0; 
     67             
     68            // Extract the OSM element ID the user wants as the center point 
     69            int id; 
     70                        try { 
     71                                id = Integer.parseInt(osmid.getText().trim()); 
     72                        } catch (NumberFormatException e1) { 
     73                    Bounds b = new Bounds(0,0,0,0); 
     74                    parent.boundingBoxChanged(b, IdNumberSelection.this); 
     75                    return; 
     76                        } 
     77            if( id == lastid ) { 
     78                return; 
     79            } 
     80            lastid = id;             
     81                         
     82                        // Use the DOM to load an element from the API, and parse out the lat/lon. 
     83                        String url = "http://api.openstreetmap.org/api/0.6/node/"+id; 
     84                        try { 
     85                                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     86                                DocumentBuilder db = dbf.newDocumentBuilder(); 
     87                                logger.info(url); 
     88                                Document doc = db.parse(url);                            
     89                                Node         node = doc.getElementsByTagName("node").item(0); 
     90                                NamedNodeMap nmap = node.getAttributes(); 
     91                                mylat = Double.parseDouble(nmap.getNamedItem("lat").getNodeValue()); 
     92                                mylon = Double.parseDouble(nmap.getNamedItem("lon").getNodeValue());                                     
     93                        } catch (ParserConfigurationException e) { 
     94                                e.printStackTrace(); 
     95                        } catch (SAXException e) { 
     96                                e.printStackTrace(); 
     97                        } catch (IOException e) { 
     98                                mylat = mylon = 0; 
     99                        }        
     100                                                 
     101            Bounds b = new Bounds(mylat-fudgelon,mylon-fudgelat,mylat+fudgelon,mylon+fudgelat); 
     102            parent.boundingBoxChanged(b, IdNumberSelection.this); 
     103        } 
     104        public void focusLost(FocusEvent e) { 
     105            refreshBounds(); 
     106        } 
     107        public void actionPerformed(ActionEvent e) { 
     108            refreshBounds(); 
     109        } 
     110    } 
     111} 
     112