Giter Site home page Giter Site logo

autobound's People

Contributors

bbloggsbott avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

autobound's Issues

Code for getting a custom area (background only)

Not the nicest code, but it works

        MapView mv = MainApplication.getMap().mapView;
        // The area to take the image from.
        MapViewState.MapViewRectangle selectedArea = mv.getState().getViewArea(r);
        // The area in east/north coordinates
        ProjectionBounds selectedInEastNorth = selectedArea.getProjectionBounds();

        Optional<Layer> layerOpt = MainApplication.getLayerManager().getLayers().stream()
                .filter(l -> l.isBackgroundLayer() && l.isVisible())
                .findFirst();
        if (layerOpt.isPresent()) {
            Layer layer = layerOpt.get();

            MainLayerManager layersToRender = new MainLayerManager();
            layersToRender.addLayer(layer);
            MapView fakeMapView = new MapView(layersToRender, null) {
                {
                    setBounds(0, 0, 1024, 1024); // < max rendering size
                    updateLocationState();
                }

                @Override
                protected boolean isVisibleOnScreen() {
                    return true;
                }

                @Override
                public Point getLocationOnScreen() {
                    return new Point(0, 0);
                }
            };

            Runnable reset = () -> {};
            // Dirty hack. This should be fixed in JOSM.
            if (layer instanceof AbstractTileSourceLayer) {
                try {
                    Field coordinateConverterField = AbstractTileSourceLayer.class.getDeclaredField("coordinateConverter");
                    coordinateConverterField.setAccessible(true);
                    Field tileSourceField = TileCoordinateConverter.class.getDeclaredField("tileSource");
                    Field settingsField = TileCoordinateConverter.class.getDeclaredField("settings");
                    TileCoordinateConverter oldConverter = (TileCoordinateConverter) coordinateConverterField.get(layer);
                    tileSourceField.setAccessible(true);
                    settingsField.setAccessible(true);
                    TileCoordinateConverter newConverter = new TileCoordinateConverter(fakeMapView,
                            (TileSource) tileSourceField.get(oldConverter),
                            (TileSourceDisplaySettings) settingsField.get(oldConverter));
                    coordinateConverterField.set(layer, newConverter);
                    reset = () -> {
                        try {
                            coordinateConverterField.set(layer, oldConverter);
                        } catch (IllegalAccessException ex) {
                            Logging.warn(ex);
                        }
                    };
                } catch (NoSuchFieldException | IllegalAccessException ex) {
                    // TODO: Show error
                    Logging.error(ex);
                }
            }

            MapViewPaintable.LayerPainter painter = layer.attachToMapView(new MapViewPaintable.MapViewEvent(fakeMapView, false));
            // Mind that this will not exactly zoom to that area, but instead to an area close to it depending on the native scale of the background layer.
            fakeMapView.zoomTo(selectedArea.getCornerBounds());
            // Now we need to find out the selected are in faked map view space
            MapViewState.MapViewRectangle toPaint = fakeMapView.getState().getPointFor(selectedInEastNorth.getMin())
                    .rectTo(fakeMapView.getState().getPointFor(selectedInEastNorth.getMax()));

            // Actual drawing
            BufferedImage image = new BufferedImage((int) toPaint.getInView().getWidth(), (int) toPaint.getInView().getHeight(),
                    BufferedImage.TYPE_BYTE_INDEXED);

            Graphics2D graphics = image.createGraphics();
            // Move so that the image matches the region we are painting.
            graphics.translate(-toPaint.getInView().getMinX(), -toPaint.getInView().getMinY());
            painter.paint(new MapViewGraphics(fakeMapView, graphics, toPaint));

            // Dispose the painter, release resources.
            graphics.dispose();
            painter.detachFromMapView(new MapViewPaintable.MapViewEvent(fakeMapView, false));
            /* TODO: We could dispose layers. But this will make some global state of JOSM inconsistent.
             * So we just let GC reclaim as many resources as possible. */
            reset.run();

            // Good for debugging / scaling: How many meters are in 100 pixel for your image
            // I don't know if your training model will improve if you normalize the images by using this, any way you should store it just in case we need it later.
            fakeMapView.getDist100Pixel();

            try {
                ImageIO.write(image, "png", new File("whatever.png"));
            } catch (IOException ex) {
                //TODO: Show error
            }
        } else {
            JOptionPane.showMessageDialog(
                    MainApplication.getMainFrame(),
                    tr("No imagery layer found."),
                    tr("Warning"),
                    JOptionPane.WARNING_MESSAGE);
        }

Mind the copyright issues: Before publishing (parts of) satellite images online, you should check if that source allows them to be published. Bing does not allow it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.