Changeset 5578 in josm


Ignore:
Timestamp:
2012-11-13T14:19:52+01:00 (11 years ago)
Author:
simon04
Message:

fix #7262 - allow searching for position in way.

The search keyword nth: have been added in order to
search for the n-th member of relations and/or n-th node of ways.
Also, nth%: is supported which searches for every n-th item, respectively.

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

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

    r5441 r5578  
    433433                , GBC.eol());
    434434            right.add(new SearchKeywordRow(hcbSearchString)
    435                 .addTitle(tr("relations"))
     435                .addTitle(tr("related objects"))
    436436                .addKeyword("child <i>expr</i>", "child ", tr("all children of objects matching the expression"), "child building")
    437437                .addKeyword("parent <i>expr</i>", "parent ", tr("all parents of objects matching the expression"), "parent bus_stop")
     438                .addKeyword("nth:<i>7</i>", "nth: ", tr("n-th member of relation and/or n-th node of way"), "nth:5 (child type:relation)")
     439                .addKeyword("nth%:<i>7</i>", "nth: ", tr("every n-th member of relation and/or every n-th node of way"), "nth%:100 (child waterway)")
    438440                , GBC.eol());
    439441            right.add(new SearchKeywordRow(hcbSearchString)
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r5184 r5578  
    101101                "changeset", "nodes", "tags", "areasize", "modified", "selected",
    102102                "incomplete", "untagged", "closed", "new", "indownloadarea",
    103                 "allindownloadarea", "inview", "allinview", "timestamp");
     103                "allindownloadarea", "inview", "allinview", "timestamp", "nth", "nth%");
    104104
    105105        @Override
     
    138138                else if ("areasize".equals(keyword))
    139139                    return new AreaSize(tokenizer);
     140                else if ("nth".equals(keyword))
     141                    return new Nth(tokenizer, false);
     142                else if ("nth%".equals(keyword))
     143                    return new Nth(tokenizer, true);
    140144                else if ("timestamp".equals(keyword)) {
    141145                    String rangeS = " " + tokenizer.readTextOrNumber() + " "; // add leading/trailing space in order to get expected split (e.g. "a--" => {"a", ""})
     
    785789        @Override public String toString() {
    786790            return "role=" + role;
     791        }
     792    }
     793
     794    /**
     795     * Matches the n-th object of a relation and/or the n-th node of a way.
     796     */
     797    private static class Nth extends Match {
     798
     799        private final int nth;
     800        private final boolean modulo;
     801
     802        public Nth(PushbackTokenizer tokenizer, boolean modulo) throws ParseError {
     803            this((int) tokenizer.readNumber(tr("Primitive id expected")), modulo);
     804        }
     805
     806        private Nth(int nth, boolean modulo) {
     807            this.nth = nth;
     808            this.modulo = modulo;
     809        }
     810
     811        @Override
     812        public boolean match(OsmPrimitive osm) {
     813            for (OsmPrimitive p : osm.getReferrers()) {
     814                Integer idx = null;
     815                if (p instanceof Way) {
     816                    Way w = (Way) p;
     817                    idx = w.getNodes().indexOf(osm);
     818                } else if (p instanceof Relation) {
     819                    Relation r = (Relation) p;
     820                    idx = r.getMemberPrimitivesList().indexOf(osm);
     821                }
     822                if (idx != null) {
     823                    if (idx.intValue() == nth || (modulo && idx.intValue() % nth == 0)) {
     824                        return true;
     825                    }
     826                }
     827            }
     828            return false;
    787829        }
    788830    }
  • trunk/src/org/openstreetmap/josm/data/osm/Relation.java

    r5266 r5578  
    1313import org.openstreetmap.josm.data.osm.visitor.Visitor;
    1414import org.openstreetmap.josm.tools.CopyList;
     15import org.openstreetmap.josm.tools.Utils;
    1516
    1617/**
     
    395396    }
    396397
     398    public List<OsmPrimitive> getMemberPrimitivesList() {
     399        return Utils.transform(getMembers(), new Utils.Function<RelationMember, OsmPrimitive>() {
     400            @Override
     401            public OsmPrimitive apply(RelationMember x) {
     402                return x.getMember();
     403            }
     404        });
     405    }
     406
    397407    @Override
    398408    public OsmPrimitiveType getType() {
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r5577 r5578  
    1919import java.security.NoSuchAlgorithmException;
    2020import java.text.MessageFormat;
     21import java.util.AbstractCollection;
     22import java.util.AbstractList;
    2123import java.util.ArrayList;
    2224import java.util.Collection;
     
    455457     */
    456458    public static <A, B> Collection<B> transform(final Collection<? extends A> c, final Function<A, B> f) {
    457         return new Collection<B>() {
     459        return new AbstractCollection<B>() {
    458460
    459461            @Override
    460462            public int size() {
    461463                return c.size();
    462             }
    463 
    464             @Override
    465             public boolean isEmpty() {
    466                 return c.isEmpty();
    467             }
    468 
    469             @Override
    470             public boolean contains(Object o) {
    471                 return c.contains(o);
    472             }
    473 
    474             @Override
    475             public Object[] toArray() {
    476                 return c.toArray();
    477             }
    478 
    479             @Override
    480             public <T> T[] toArray(T[] a) {
    481                 return c.toArray(a);
    482             }
    483 
    484             @Override
    485             public String toString() {
    486                 return c.toString();
    487464            }
    488465
     
    509486                };
    510487            }
     488        };
     489    }
     490
     491    /**
     492     * Transforms the list {@code l} into an unmodifiable list and
     493     * applies the {@link Function} {@code f} on each element upon access.
     494     * @param <A> class of input collection
     495     * @param <B> class of transformed collection
     496     * @param l a collection
     497     * @param f a function that transforms objects of {@code A} to objects of {@code B}
     498     * @return the transformed unmodifiable list
     499     */
     500    public static <A, B> List<B> transform(final List<? extends A> l, final Function<A, B> f) {
     501        return new AbstractList<B>() {
     502
    511503
    512504            @Override
    513             public boolean add(B e) {
    514                 throw new UnsupportedOperationException();
     505            public int size() {
     506                return l.size();
    515507            }
    516508
    517509            @Override
    518             public boolean remove(Object o) {
    519                 throw new UnsupportedOperationException();
    520             }
    521 
    522             @Override
    523             public boolean containsAll(Collection<?> c) {
    524                 throw new UnsupportedOperationException();
    525             }
    526 
    527             @Override
    528             public boolean addAll(Collection<? extends B> c) {
    529                 throw new UnsupportedOperationException();
    530             }
    531 
    532             @Override
    533             public boolean removeAll(Collection<?> c) {
    534                 throw new UnsupportedOperationException();
    535             }
    536 
    537             @Override
    538             public boolean retainAll(Collection<?> c) {
    539                 throw new UnsupportedOperationException();
    540             }
    541 
    542             @Override
    543             public void clear() {
    544                 throw new UnsupportedOperationException();
    545             }
     510            public B get(int index) {
     511                return f.apply(l.get(index));
     512            }
     513
     514
    546515        };
    547516    }
Note: See TracChangeset for help on using the changeset viewer.