src/main/java/de/blau/android/osm/StorageDelegator.java
/**
     * Paste the contents of the clipboard to coordinates
     * 
     * If the content was copied to the clipboard new elements will be created.
     * 
     * @param lat latitude in WGS84*1E7 degrees
     * @param lon longitude in WGS84*1E7 degrees
     * @return the contents or null is the clipboard was empty
     */
Paste the contents of the clipboard to coordinates

If the content was copied to the clipboard new elements will be created.

@param lat latitude in WGS84*1E7 degrees
@param lon longitude in WGS84*1E7 degrees
@return the contents or null is the clipboard was empty
Paste the contents of the clipboard to coordinates If the content was copied to the clipboard new elements will be created . @param lat latitude in WGS84 * 1E7 degrees @param lon longitude in WGS84 * 1E7 degrees @return the contents or null is the clipboard was empty
@Nullable
    public List<OsmElement> pasteFromClipboard(int lat, int lon) {
        List<OsmElement> elements = clipboard.pasteFrom();
        if (elements.isEmpty()) {
            return null;
        }
        Collections.sort(elements, new NwrComparator()); 
        List<OsmElement> result = new ArrayList<>();
        boolean copy = !clipboard.isEmpty();
        int deltaLat = lat - clipboard.getSelectionLat();
        int deltaLon = lon - clipboard.getSelectionLon();
        Map<Node, Node> newNodes = new HashMap<>(); 
        for (OsmElement e : elements) {
            if (copy) { 
                if (e instanceof Node) {
                    Node newNode = factory.createNodeWithNewId(((Node) e).getLat() + deltaLat, ((Node) e).getLon() + deltaLon);
                    newNode.setTags(e.getTags());
                    insertElementSafe(newNode);
                    newNodes.put((Node) e, newNode);
                    e = newNode;
                } else if (e instanceof Way) {
                    Way newWay = factory.createWayWithNewId();
                    undo.save(newWay); 
                    newWay.setTags(e.getTags());
                    List<Node> nodeList = ((Way) e).getNodes();
                    Set<Node> nodes = new HashSet<>(nodeList);
                    for (Node nd : nodes) {
                        if (!newNodes.containsKey(nd)) {
                            Node newNode = factory.createNodeWithNewId(nd.getLat() + deltaLat, nd.getLon() + deltaLon);
                            newNode.setTags(nd.getTags());
                            insertElementSafe(newNode);
                            newNodes.put(nd, newNode);
                        }
                    }
                    for (Node nd : nodeList) {
                        newWay.addNode(newNodes.get(nd));
                    }
                    insertElementSafe(newWay);
                    e = newWay;
                }
            } else { 
                if (currentStorage.contains(e)) {
                    Log.e(DEBUG_TAG, "Attempt to paste from cut, but element is already present");
                    clipboard.reset();
                    return null;
                }
                undo.save(e);
                if (e instanceof Node) {
                    ((Node) e).setLat(((Node) e).getLat() + deltaLat);
                    ((Node) e).setLon(((Node) e).getLon() + deltaLon);
                    newNodes.put((Node) e, null);
                } else if (e instanceof Way) {
                    Set<Node> nodes = new HashSet<>(((Way) e).getNodes());
                    for (Node nd : nodes) {
                        if (!newNodes.containsKey(nd)) {
                            undo.save(nd);
                            nd.setLat(nd.getLat() + deltaLat);
                            nd.setLon(nd.getLon() + deltaLon);
                            nd.updateState(nd.getOsmId() < 0 ? OsmElement.STATE_CREATED : OsmElement.STATE_MODIFIED);
                            insertElementSafe(nd);
                            newNodes.put(nd, null);
                        }
                    }
                    ((Way) e).invalidateBoundingBox();
                }
                insertElementSafe(e);
                e.updateState(e.getOsmId() < 0 ? OsmElement.STATE_CREATED : OsmElement.STATE_MODIFIED);
            }
            result.add(e);
        }
        return result;
    }
@ Nullable public List < OsmElement > pasteFromClipboard ( int lat , int lon ) { List < OsmElement > elements = clipboard . pasteFrom ( ) ; if ( elements . isEmpty ( ) ) { return null ; } Collections . sort ( elements , new NwrComparator ( ) ) ; List < OsmElement > result = new ArrayList < > ( ) ; boolean copy = ! clipboard . isEmpty ( ) ; int deltaLat = lat - clipboard . getSelectionLat ( ) ; int deltaLon = lon - clipboard . getSelectionLon ( ) ; Map < Node , Node > newNodes = new HashMap < > ( ) ; for ( OsmElement e : elements ) { if ( copy ) { if ( e instanceof Node ) { Node newNode = factory . createNodeWithNewId ( ( ( Node ) e ) . getLat ( ) + deltaLat , ( ( Node ) e ) . getLon ( ) + deltaLon ) ; newNode . setTags ( e . getTags ( ) ) ; insertElementSafe ( newNode ) ; newNodes . put ( ( Node ) e , newNode ) ; e = newNode ; } else if ( e instanceof Way ) { Way newWay = factory . createWayWithNewId ( ) ; undo . save ( newWay ) ; newWay . setTags ( e . getTags ( ) ) ; List < Node > nodeList = ( ( Way ) e ) . getNodes ( ) ; Set < Node > nodes = new HashSet < > ( nodeList ) ; for ( Node nd : nodes ) { if ( ! newNodes . containsKey ( nd ) ) { Node newNode = factory . createNodeWithNewId ( nd . getLat ( ) + deltaLat , nd . getLon ( ) + deltaLon ) ; newNode . setTags ( nd . getTags ( ) ) ; insertElementSafe ( newNode ) ; newNodes . put ( nd , newNode ) ; } } for ( Node nd : nodeList ) { newWay . addNode ( newNodes . get ( nd ) ) ; } insertElementSafe ( newWay ) ; e = newWay ; } } else { if ( currentStorage . contains ( e ) ) { Log . e ( DEBUG_TAG , "Attempt to paste from cut, but element is already present" ) ; clipboard . reset ( ) ; return null ; } undo . save ( e ) ; if ( e instanceof Node ) { ( ( Node ) e ) . setLat ( ( ( Node ) e ) . getLat ( ) + deltaLat ) ; ( ( Node ) e ) . setLon ( ( ( Node ) e ) . getLon ( ) + deltaLon ) ; newNodes . put ( ( Node ) e , null ) ; } else if ( e instanceof Way ) { Set < Node > nodes = new HashSet < > ( ( ( Way ) e ) . getNodes ( ) ) ; for ( Node nd : nodes ) { if ( ! newNodes . containsKey ( nd ) ) { undo . save ( nd ) ; nd . setLat ( nd . getLat ( ) + deltaLat ) ; nd . setLon ( nd . getLon ( ) + deltaLon ) ; nd . updateState ( nd . getOsmId ( ) < 0 ? OsmElement . STATE_CREATED : OsmElement . STATE_MODIFIED ) ; insertElementSafe ( nd ) ; newNodes . put ( nd , null ) ; } } ( ( Way ) e ) . invalidateBoundingBox ( ) ; } insertElementSafe ( e ) ; e . updateState ( e . getOsmId ( ) < 0 ? OsmElement . STATE_CREATED : OsmElement . STATE_MODIFIED ) ; } result . add ( e ) ; } return result ; }
Paste the contents of the clipboard to coordinates
Paste the contents of the clipboard to coordinates
// enforce NWR order // every node needs to only be transformed once // if the clipboard isn't empty now we need to clone the element // paste from copy // do this before we create and add nodes // this is slightly complicated because we need to handle cases with potentially broken geometry // allocate and set the position of the new nodes // now add them to the new way // paste from cut