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

nl.cloudfarming.client.geoviewer.jxmap.render.JXMapGeoTranslator 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.render;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import nl.cloudfarming.client.geoviewer.render.GeoTranslator;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;

/**
 * JXMap implementation to translate geo to pixel and visa versa
 * @author Merijn Zengers
 */
public class JXMapGeoTranslator implements GeoTranslator{

    private final JXMapViewer mapViewer;

    public JXMapGeoTranslator(JXMapViewer mapViewer) {
        this.mapViewer = mapViewer;
    }
    
    
    /**
     * Translate Coordinate to pixel
     * @param canvas mapviewer is used instead, should be null
     * @param boundingBox mapviewer is used instead, should be null
     * @param coordinate coordinate to translate for current map position
     * @return the translated pixel
     */
    @Override
    public Point2D geoToPixel(Rectangle canvas, Geometry boundingBox, Coordinate coordinate) {
        GeoPosition geopoint = new GeoPosition(coordinate.y, coordinate.x);
        //convert from geo to world bitmap
        Point2D pt = mapViewer.getTileFactory().geoToPixel(geopoint, mapViewer.getZoom());
        //convert from world bitmap to local 
        Rectangle bounds = mapViewer.getViewportBounds();
        return new Point2D.Double(pt.getX() - bounds.getX(),pt.getY() - bounds.getY());
    }

    /**
     * Translate pixel to geoPosition
     * @param canvas mapviewer is used instead, should be null
     * @param boundingBox mapviewer is used instead, should be null
     * @param pixel the pixel to transform
     * @return the translated GeoPosition
     */
    @Override
    public GeoPosition pixelToGeo(Rectangle canvas, Geometry boundingBox, Point2D pixel) {
        Rectangle viewportBounds = mapViewer.getViewportBounds();
        int mapX = (int) (viewportBounds.getX() + pixel.getX());
        int mapY = (int) (viewportBounds.getY() + pixel.getY());
        Point2D newPoint = new java.awt.Point(mapX, mapY);
        GeoPosition geoPosition = mapViewer.getTileFactory().pixelToGeo(newPoint, mapViewer.getZoom());
        return geoPosition;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy