All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.cloudfarming.client.geoviewer.jxmap.map.LayerPainter Maven / Gradle / Ivy

Go to download

AgroSense geoviewer JXMap implementation. Contains a map/geoviewer TopComponent based on the JXMap classes from swingx.

There is a newer version: 13.03-beta
Show newest version
/**
 * Copyright (C) 2010-2012 Agrosense [email protected]
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.map;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.logging.Level;
import java.util.logging.Logger;
import nl.cloudfarming.client.geoviewer.Geographical;
import nl.cloudfarming.client.geoviewer.Geometrical;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.jxmap.render.JXMapGeoTranslator;
import nl.cloudfarming.client.geoviewer.render.GeographicalWidget;
import nl.cloudfarming.client.geoviewer.render.GeometryRenderer;
import nl.cloudfarming.client.geoviewer.render.GeometryRendererFactory;
import nl.cloudfarming.client.geoviewer.render.RasterRenderer;
import nl.cloudfarming.client.geoviewer.render.RasterRendererFactory;
import org.geotools.coverage.grid.GridCoverage2D;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.painter.Painter;
import org.openide.nodes.Node;

/**
 *
 * LayerPainter can be called from both a layerPanel directly in wich case the
 * argument will be a layerPanel (ie complies to Painter), but also
 * through a repaint of the main map, which is a jxmapviewer. In the latter case
 * the call complies to Painter which will case a
 * classCastException. The painters object is set to JXPanel as shared object
 * between the two
 *
 * @deprecated {@link GeographicalWidget} should be used to paint things on
 * layers
 * @author Timon Veenstra
 */
@Deprecated
public class LayerPainter implements Painter {

    private final Node layerNode;
    private static final Logger LOG = Logger.getLogger("nl.cloudfarming.client.geoviewer");

    public LayerPainter(Node layerNode) {
        this.layerNode = layerNode;
    }

    @Override
    public void paint(Graphics2D graphics, JXPanel panel, int width, int height) {
        Graphics2D g = (Graphics2D) graphics.create();
        if (panel instanceof LayerPanel) {
            LayerPanel layerPanel = (LayerPanel) panel;
            JXMapViewer map = layerPanel.getMapViewer();

            Geographical geographical = layerNode.getLookup().lookup(Geographical.class);
            if (geographical != null) {
                LOG.finest("Painting geographical contained in parent node");
                paintGeographical(geographical, map, g, new Rectangle(width, height));
            }
            LOG.log(Level.FINEST, "Searching in {0} children for Geographical''s", layerNode.getChildren().getNodesCount());
            for (Node child : layerNode.getChildren().getNodes()) {
                Geographical chilGeographical = child.getLookup().lookup(Geographical.class);
                paintGeographical(chilGeographical, map, g, new Rectangle(width, height));
            }
        }
        g.dispose();

    }

    private void paintGeographical(Geographical geographical, JXMapViewer map, Graphics2D g, Rectangle viewport) {

        Layer layer = layerNode.getLookup().lookup(Layer.class);
        assert geographical != null : "Geographical should not be null";
        if (geographical instanceof Geometrical) {
            //TODO optimize and use geographical.getRenderObject(envelope);?
            Geometry geometry = ((Geometrical) geographical).getGeometry();
            if (geometry != null) {
                GeometryRenderer renderer = GeometryRendererFactory.getRenderer(geometry);
                renderer.render(geometry, geometry.getBoundary(), viewport, new JXMapGeoTranslator(map), g);
            } else {
                LOG.log(Level.WARNING, "Unrenderable object {0} geometry is null", geographical);
            }
        } else {
//            Rectangle bounds = map.getBounds();
//            GeoPosition upLeft = map.getTileFactory().pixelToGeo(new Point(bounds.x, bounds.y), map.getZoom());
//            GeoPosition downRight = map.getTileFactory().pixelToGeo(new Point(bounds.x + bounds.width, bounds.y + bounds.height), map.getZoom());
//            Coordinate c1 = new Coordinate(upLeft.getLatitude(), upLeft.getLongitude());
//            Coordinate c2 = new Coordinate(downRight.getLatitude(), downRight.getLongitude());
//            Envelope envelope = new Envelope(c1, c2);
//            Object objectToRender = geographical.getRenderObject(envelope);
            Object objectToRender = geographical.getRenderObject(null);

            if (objectToRender instanceof GridCoverage2D) {
                GridCoverage2D raster = (GridCoverage2D) objectToRender;
                RasterRenderer rasterRenderer = RasterRendererFactory.getRenderer(raster);
                rasterRenderer.paint(raster, viewport, g, layer.getPalette(), new JXMapGeoTranslator(map));
            } else {
                LOG.log(Level.WARNING, "Unrenderable object {0},  unable to find suitable renderer", objectToRender);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy