Ticket #16998: 16998.1.patch

File 16998.1.patch, 4.8 KB (added by taylor.smock, 4 years ago)

Return primitives instead of ids, add method to convert primitives to strings

  • src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java

     
    148148    private static OsmPrimitiveType getOsmPrimitiveType(char firstChar) {
    149149        return firstChar == 'n' ? OsmPrimitiveType.NODE : firstChar == 'w' ? OsmPrimitiveType.WAY : OsmPrimitiveType.RELATION;
    150150    }
     151
     152    /**
     153     * Convert a primitive to a simple id
     154     *
     155     * @param primitive The primitive to convert
     156     * @return The type (may be n, w, or r, or something else) + the id (e.g., w42)
     157     * @since xxx
     158     */
     159    public static String toSimpleId(IPrimitive primitive) {
     160        if (primitive instanceof Relation) {
     161            return "r" + primitive.getOsmId();
     162        } else if (primitive instanceof Way) {
     163            return "w" + primitive.getOsmId();
     164        } else if (primitive instanceof Node) {
     165            return "n" + primitive.getOsmId();
     166        }
     167        return primitive.getType().toString() + primitive.getOsmId();
     168    }
    151169}
  • src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java

     
    2424import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2525import org.openstreetmap.josm.data.osm.Relation;
    2626import org.openstreetmap.josm.data.osm.RelationMember;
     27import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
    2728import org.openstreetmap.josm.data.osm.Way;
    2829import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    2930import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
     
    515516    }
    516517
    517518    /**
     519     * Gets a list of all OSM id's of the object's parent(s) with a specified key.
     520     *
     521     * @param env      the environment
     522     * @param key      the OSM key
     523     * @param keyValue the regex value of the OSM key
     524     * @return a list of non-null values of the OSM id's from the object's parent(s)
     525     * @since xxx
     526     */
     527    public static List<IPrimitive> parent_osm_primitives(final Environment env, String key, String keyValue) {
     528        if (env.parent == null) {
     529            if (env.osm != null) {
     530                final ArrayList<IPrimitive> parents = new ArrayList<>();
     531                for (IPrimitive parent : env.osm.getReferrers()) {
     532                    if ((key == null || parent.get(key) != null)
     533                            && (keyValue == null || regexp_test(keyValue, parent.get(key)))) {
     534                        parents.add(parent);
     535                    }
     536                }
     537                return new ArrayList<>(parents);
     538            }
     539            return Collections.emptyList();
     540        }
     541        return Collections.singletonList(env.parent);
     542    }
     543
     544    /**
     545     * Gets a list of all OSM id's of the object's parent(s) with a specified key.
     546     *
     547     * @param env the environment
     548     * @param key the OSM key
     549     * @return a list of non-null values of the OSM id's from the object's parent(s)
     550     * @since xxx
     551     */
     552    public static List<IPrimitive> parent_osm_primitives(final Environment env, String key) { // NO_UCD (unused code)
     553        return parent_osm_primitives(env, key, null);
     554    }
     555
     556    /**
     557     * Gets a list of all OSM id's of the object's parent(s).
     558     *
     559     * @param env the environment
     560     * @return a list of non-null values of the OSM id's from the object's parent(s)
     561     * @since xxx
     562     */
     563    public static List<IPrimitive> parent_osm_primitives(final Environment env) { // NO_UCD (unused code)
     564        return parent_osm_primitives(env, null, null);
     565    }
     566
     567    /**
     568     * Convert Primitives to a string
     569     *
     570     * @param primitives The primitives to convert
     571     * @return A list of strings in the format type + id (in the list order)
     572     * @see SimplePrimitiveId#toSimpleId
     573     * @since xxx
     574     */
     575    public static List<String> convert_primitives_to_string(List<IPrimitive> primitives) {
     576        return primitives.stream().map(Functions::convert_primitive_to_string).collect(Collectors.toList());
     577    }
     578
     579    /**
     580     * Convert a primitive to a string
     581     *
     582     * @param primitive The primitive to convert
     583     * @return A string in the format type + id
     584     * @see SimplePrimitiveId#toSimpleId
     585     * @since xxx
     586     */
     587    public static String convert_primitive_to_string(IPrimitive primitive) {
     588        return SimplePrimitiveId.toSimpleId(primitive);
     589    }
     590
     591    /**
    518592     * Returns the lowest distance between the OSM object and a GPX point
    519593     * <p>
    520594     * @param env the environment