001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tool.edit.mapgraphic;
016:
017: import java.awt.Color;
018: import java.awt.Point;
019:
020: import net.refractions.udig.mapgraphic.MapGraphic;
021: import net.refractions.udig.mapgraphic.MapGraphicContext;
022: import net.refractions.udig.project.ILayer;
023: import net.refractions.udig.tool.edit.internal.Messages;
024: import net.refractions.udig.tools.edit.EditPlugin;
025: import net.refractions.udig.tools.edit.preferences.PreferenceUtil;
026: import net.refractions.udig.ui.graphics.ViewportGraphics;
027:
028: import org.geotools.referencing.CRS;
029: import org.geotools.referencing.crs.DefaultGeographicCRS;
030: import org.opengis.referencing.operation.MathTransform;
031:
032: import com.vividsolutions.jts.geom.Coordinate;
033: import com.vividsolutions.jts.geom.Envelope;
034:
035: /**
036: * Draws the grid on the map.
037: *
038: * @author Jesse
039: * @since 1.1.0
040: */
041: public class GridMapGraphic implements MapGraphic {
042:
043: private Color color = new Color(0, 0, 0, 100);
044:
045: public void draw(MapGraphicContext context) {
046: double[] gridSize = PreferenceUtil.instance().getGridSize();
047: try {
048: MathTransform mt = CRS.findMathTransform(
049: DefaultGeographicCRS.WGS84, context.getCRS(), true);
050:
051: if (!mt.isIdentity()) {
052: double x = gridSize[0] / 2.0;
053: double y = gridSize[1] / 2.0;
054: double[] toTransform = new double[] { -x, -y, x, y };
055: double[] dest = new double[4];
056: mt.transform(toTransform, 0, dest, 0, 2);
057: gridSize = new double[] { Math.abs(dest[2] - dest[0]),
058: Math.abs(dest[3] - dest[1]) };
059: }
060: } catch (Exception e) {
061: EditPlugin.log("", e); //$NON-NLS-1$
062: }
063:
064: Envelope bounds = context.getViewportModel().getBounds();
065: double newx = Math.round(bounds.getMinX() / gridSize[0])
066: * gridSize[0];
067: double newy = Math.round(bounds.getMaxY() / gridSize[1])
068: * gridSize[1];
069: Coordinate coord = new Coordinate(newx, newy);
070: while (context.worldToPixel(coord).x < 0) {
071: coord.x += gridSize[0];
072: }
073: while (context.worldToPixel(coord).y < 0) {
074: coord.y -= gridSize[1];
075: }
076: context.getGraphics().setColor(color);
077: context.getGraphics().setStroke(ViewportGraphics.LINE_DOT, 1);
078: Point pixel = null;
079: while (true) {
080: pixel = context.worldToPixel(coord);
081: coord.x += gridSize[0];
082: coord.y -= gridSize[1];
083: Point next = context.worldToPixel(coord);
084: if (next.x - pixel.x < 2 || next.y - pixel.y < 2) {
085: context.getLayer().setStatus(ILayer.WARNING);
086: context.getLayer().setStatusMessage(
087: Messages.GridMapGraphic_grids_too_close);
088: break;
089: }
090: if ((pixel.x >= context.getMapDisplay().getWidth() && pixel.y >= context
091: .getMapDisplay().getHeight()))
092: break;
093:
094: if (pixel.x < context.getMapDisplay().getWidth())
095: context.getGraphics().drawLine(pixel.x, 0, pixel.x,
096: context.getMapDisplay().getHeight());
097: if (pixel.y < context.getMapDisplay().getHeight())
098: context.getGraphics().drawLine(0, pixel.y,
099: context.getMapDisplay().getWidth(), pixel.y);
100: pixel = next;
101: }
102: }
103:
104: }
|