Changeset 4372 in josm


Ignore:
Timestamp:
Aug 28, 2011 1:54:11 PM (22 months ago)
Author:
simon04
Message:

fix 4622 - add inDownloadedArea, allinDownloadedArea to search

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r4347 r4372  
    88import java.io.StringReader; 
    99import java.text.Normalizer; 
     10import java.util.Collection; 
    1011import java.util.regex.Matcher; 
    1112import java.util.regex.Pattern; 
     
    6162    abstract public static class Match { 
    6263        abstract public boolean match(OsmPrimitive osm); 
     64 
     65        /** 
     66         * Tests whether one of the primitives matches. 
     67         */ 
     68        protected boolean existsMatch(Collection<? extends OsmPrimitive> primitives) { 
     69            for (OsmPrimitive p : primitives) { 
     70                if (match(p)) { 
     71                    return true; 
     72                } 
     73            } 
     74            return false; 
     75        } 
     76 
     77        /** 
     78         * Tests whether all primitives match. 
     79         */ 
     80        protected boolean forallMatch(Collection<? extends OsmPrimitive> primitives) { 
     81            for (OsmPrimitive p : primitives) { 
     82                if (!match(p)) { 
     83                    return false; 
     84                } 
     85            } 
     86            return true; 
     87        } 
    6388    } 
    6489 
     
    695720        protected String getCountString() { 
    696721            return "area"; 
     722        } 
     723    } 
     724 
     725    /** 
     726     * Matches data in source area ("downloaded area"). 
     727     */ 
     728    private static class InDataSourceArea extends Match { 
     729 
     730        private final java.awt.geom.Area dataSourceArea = Main.main.getCurrentDataSet().getDataSourceArea(); 
     731        private final boolean all; 
     732 
     733        /** 
     734         * @param all if true, all way nodes or relation members have to be within source area;if false, one suffices. 
     735         */ 
     736        public InDataSourceArea(boolean all) { 
     737            this.all = all; 
     738        } 
     739 
     740        @Override 
     741        public boolean match(OsmPrimitive osm) { 
     742            if (!osm.isUsable()) { 
     743                return false; 
     744            } else if (osm instanceof Node) { 
     745                return dataSourceArea.contains(((Node) osm).getCoor()); 
     746            } else if (osm instanceof Way) { 
     747                Collection<Node> nodes = ((Way) osm).getNodes(); 
     748                return all ? forallMatch(nodes) : existsMatch(nodes); 
     749            } else if (osm instanceof Relation) { 
     750                Collection<OsmPrimitive> primitives = ((Relation) osm).getMemberPrimitives(); 
     751                return all ? forallMatch(primitives) : existsMatch(primitives); 
     752            } else { 
     753                return false; 
     754            } 
    697755        } 
    698756    } 
     
    795853            else if ("parent".equals(key)) 
    796854                return new Parent(parseFactor()); 
     855            else if ("inDownloadedArea".equals(key)) 
     856                return new InDataSourceArea(false); 
     857            else if ("allinDownloadedArea".equals(key)) 
     858                return new InDataSourceArea(true); 
    797859            else 
    798860                return new Any(key, regexSearch, caseSensitive); 
Note: See TracChangeset for help on using the changeset viewer.