/[schmitzm]/trunk/src/skrueger/geotools/XMapPane.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/XMapPane.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 420 by alfonx, Thu Oct 1 20:22:48 2009 UTC revision 504 by alfonx, Tue Oct 27 12:57:58 2009 UTC
# Line 65  import org.geotools.renderer.GTRenderer; Line 65  import org.geotools.renderer.GTRenderer;
65  import org.geotools.renderer.label.LabelCacheImpl;  import org.geotools.renderer.label.LabelCacheImpl;
66  import org.geotools.renderer.lite.LabelCache;  import org.geotools.renderer.lite.LabelCache;
67  import org.geotools.renderer.lite.StreamingRenderer;  import org.geotools.renderer.lite.StreamingRenderer;
 import org.geotools.renderer.shape.ShapefileRenderer;  
68  import org.opengis.filter.FilterFactory2;  import org.opengis.filter.FilterFactory2;
69  import org.opengis.referencing.crs.CoordinateReferenceSystem;  import org.opengis.referencing.crs.CoordinateReferenceSystem;
70    
71    import schmitzm.geotools.JTSUtil;
72  import schmitzm.swing.SwingUtil;  import schmitzm.swing.SwingUtil;
73    
74  import com.vividsolutions.jts.geom.Coordinate;  import com.vividsolutions.jts.geom.Coordinate;
# Line 77  import com.vividsolutions.jts.geom.Geome Line 77  import com.vividsolutions.jts.geom.Geome
77    
78  public class JMapPane extends JPanel implements MouseListener,  public class JMapPane extends JPanel implements MouseListener,
79                  MouseMotionListener, PropertyChangeListener, MapLayerListListener {                  MouseMotionListener, PropertyChangeListener, MapLayerListListener {
80          private static Logger LOGGER = Logger.getLogger(JMapPane.class.getName());          private static Logger LOGGER = Logger.getLogger(JMapPane.class);
81    
82          private static final long serialVersionUID = -8647971481359690499L;          private static final long serialVersionUID = -8647971481359690499L;
83    
# Line 230  public class JMapPane extends JPanel imp Line 230  public class JMapPane extends JPanel imp
230                  Map<Object, Object> hints = new HashMap<Object, Object>();                  Map<Object, Object> hints = new HashMap<Object, Object>();
231    
232                  this.renderer = renderer;                  this.renderer = renderer;
233                    // MS: Apply hint also for ShapeFileRenderer
234                  if (renderer instanceof StreamingRenderer                  // if (renderer instanceof StreamingRenderer) {
235                                  || renderer instanceof ShapefileRenderer) {                  hints = renderer.getRendererHints();
236                          hints = renderer.getRendererHints();                  if (hints == null) {
237                          if (hints == null) {                          hints = new HashMap<Object, Object>();
238                                  hints = new HashMap<Object, Object>();                  }
239                          }                  if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {
240                          if (hints.containsKey(StreamingRenderer.LABEL_CACHE_KEY)) {                          labelCache = (LabelCache) hints
241                                  labelCache = (LabelCache) hints                                          .get(StreamingRenderer.LABEL_CACHE_KEY);
242                                                  .get(StreamingRenderer.LABEL_CACHE_KEY);                  } else {
243                          } else {                          hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);
                                 hints.put(StreamingRenderer.LABEL_CACHE_KEY, labelCache);  
                         }  
   
                         hints.put("memoryPreloadingEnabled", Boolean.TRUE);  
   
                         renderer.setRendererHints(hints);  
244                  }                  }
245    
246                  // this.highlightRenderer = new StreamingRenderer();                  hints.put("memoryPreloadingEnabled", Boolean.TRUE);
                 // this.selectionRenderer = new StreamingRenderer();  
   
                 // highlightRenderer.setRendererHints(hints);  
                 // selectionRenderer.setRendererHints(hints);  
247    
248                  // renderer.setRendererHints(hints);                  renderer.setRendererHints(hints);
249                    // }
250    
251                  if (this.context != null) {                  if (this.context != null) {
252                          this.renderer.setContext(this.context);                          this.renderer.setContext(this.context);
# Line 333  public class JMapPane extends JPanel imp Line 324  public class JMapPane extends JPanel imp
324                                  reset = false; /* forget about the reset */                                  reset = false; /* forget about the reset */
325                                  oldRect = r; /* store what the current size is */                                  oldRect = r; /* store what the current size is */
326    
327                                  mapArea = fixAspectRatio(r, mapArea, false);                                  mapArea = JTSUtil.fixAspectRatio(r, mapArea, false);
328                          }                          }
329                  }                  }
330    
# Line 369  public class JMapPane extends JPanel imp Line 360  public class JMapPane extends JPanel imp
360                  ((Graphics2D) g).drawImage(baseImage, 0, 0, this);                  ((Graphics2D) g).drawImage(baseImage, 0, 0, this);
361          }          }
362    
         /**  
          * Returns an {@link Envelope} that has the same aspect ratio as the given rectangle  
          * @param grow  
          *            If <code>true</code>, than the area will be enlarged to match  
          *            the aspect ratio. If <code>false</code>, it will only shrink.  
          */  
         private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea,  
                         boolean grow) {  
   
                 final double mapWidth = mapArea.getWidth(); /* get the extent of the map */  
                 final double mapHeight = mapArea.getHeight();  
                 final double scaleX = r.getWidth() / mapArea.getWidth(); /*  
                                                                                                                                  * calculate the  
                                                                                                                                  * new scale  
                                                                                                                                  */  
   
                 final double scaleY = r.getHeight() / mapArea.getHeight();  
                 double scale = 1.0; // stupid compiler!  
   
                 if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) {  
                         scale = scaleX;  
                 } else {  
                         scale = scaleY;  
                 }  
   
                 /* calculate the difference in width and height of the new extent */  
                 final double deltaX = /* Math.abs */((r.getWidth() / scale) - mapWidth);  
                 final double deltaY = /* Math.abs */((r.getHeight() / scale) - mapHeight);  
   
                 /*  
                  * System.out.println("delta x " + deltaX);  
                  * System.out.println("delta y " + deltaY);  
                  */  
   
                 /* create the new extent */  
                 final Coordinate ll = new Coordinate(  
                                 mapArea.getMinX() - (deltaX / 2.0), mapArea.getMinY()  
                                                 - (deltaY / 2.0));  
                 final Coordinate ur = new Coordinate(  
                                 mapArea.getMaxX() + (deltaX / 2.0), mapArea.getMaxY()  
                                                 + (deltaY / 2.0));  
   
                 return new Envelope(ll, ur);  
         }  
   
363          public void mouseClicked(final MouseEvent e) {          public void mouseClicked(final MouseEvent e) {
364                  if (mapArea == null)                  if (mapArea == null)
365                          return;                          return;
# Line 622  public class JMapPane extends JPanel imp Line 568  public class JMapPane extends JPanel imp
568                          final int y2, final MouseEvent e) {                          final int y2, final MouseEvent e) {
569    
570                  /****                  /****
571                   * If no layer is availabe we dont want a NullPointerException                   * If no layers exist, we ignore the drag.
572                   */                   */
573                  if (mapArea == null)                  if (context.getLayerCount() <= 0) {
574                          return;                          return;
575                    }
576    
577                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
578                  // + x2 + "," + y2);                  // + x2 + "," + y2);
# Line 672  public class JMapPane extends JPanel imp Line 619  public class JMapPane extends JPanel imp
619                          final Coordinate ur = new Coordinate(right, top);                          final Coordinate ur = new Coordinate(right, top);
620                          // xulu.sc                          // xulu.sc
621                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
622    
623                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
624                          // xulu.ec                          // xulu.ec
625                  } else if (state == JMapPane.ZoomIn) {                  } else if (state == JMapPane.ZoomIn) {
# Line 916  public class JMapPane extends JPanel imp Line 864  public class JMapPane extends JPanel imp
864    
865                  /**                  /**
866                   * Correct the aspect Ratio before we check the rest. Otherwise we might                   * Correct the aspect Ratio before we check the rest. Otherwise we might
867                   * easily fail.                   * easily fail. We allow to grow here, because we don't check against
868                     * the maxExtend
869                   */                   */
870                  env = fixAspectRatio(this.getBounds(), env, false);                  env = JTSUtil.fixAspectRatio(this.getBounds(), env, true);
871    
872                  final double scale = env.getWidth() / getWidth();                  final double scale = env.getWidth() / getWidth();
873                  final double centerX = env.getMinX() + env.getWidth() / 2.;                  final double centerX = env.getMinX() + env.getWidth() / 2.;
# Line 955  public class JMapPane extends JPanel imp Line 904  public class JMapPane extends JPanel imp
904                  }                  }
905    
906                  Envelope maxAllowedExtend = getMaxExtend();                  Envelope maxAllowedExtend = getMaxExtend();
907                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)) {                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)
908                                    && newArea != null && !newArea.isNull()
909                                    && !Double.isNaN(newArea.getMinX())
910                                    && !Double.isNaN(newArea.getMaxX())
911                                    && !Double.isNaN(newArea.getMinY())
912                                    && !Double.isNaN(newArea.getMaxY())) {
913                          /*                          /*
914                           * If a maxExtend is set, we have to honour that...                           * If a maxExtend is set, we have to honour that...
915                           */                           */
# Line 963  public class JMapPane extends JPanel imp Line 917  public class JMapPane extends JPanel imp
917                          // Exceeds top? Move down and maybe cut                          // Exceeds top? Move down and maybe cut
918                          if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {                          if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
919                                  double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();                                  double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
920                                  LOGGER.debug("Moving area down by " + divY);                                  // LOGGER.debug("Moving area down by " + divY);
921    
922                                  newArea = new Envelope(new Coordinate(newArea.getMinX(),                                  newArea = new Envelope(new Coordinate(newArea.getMinX(),
923                                                  newArea.getMinY() - divY), new Coordinate(newArea                                                  newArea.getMinY() - divY), new Coordinate(newArea
924                                                  .getMaxX(), newArea.getMaxY() - divY));                                                  .getMaxX(), newArea.getMaxY() - divY));
925    
926                                  if (newArea.getMinY() < maxAllowedExtend.getMinY()) {                                  if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
927                                          LOGGER.debug("Now it exeeds the bottom border.. cut!");                                          // LOGGER.debug("Now it exeeds the bottom border.. cut!");
928                                          // And cut the bottom if it moved out of the area                                          // And cut the bottom if it moved out of the area
929                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),
930                                                          maxAllowedExtend.getMinY()), new Coordinate(newArea                                                          maxAllowedExtend.getMinY()), new Coordinate(newArea
931                                                          .getMaxX(), newArea.getMaxY()));                                                          .getMaxX(), newArea.getMaxY()));
932    
933                                          LOGGER.debug("and fix aspect ratio");                                          // LOGGER.debug("and fix aspect ratio");
934    
935                                          newArea = fixAspectRatio(this.getBounds(), newArea, false);                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
936                                                            false);
937                                  }                                  }
938                          }                          }
939    
940                          // Exceeds bottom? Move up and maybe cut                          // Exceeds bottom? Move up and maybe cut
941                          if (newArea.getMinY() < maxAllowedExtend.getMinY()) {                          if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
942                                  double divY = newArea.getMinY() - maxAllowedExtend.getMinY();                                  double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
943                                  LOGGER.debug("Moving area up by " + divY);                                  // LOGGER.debug("Moving area up by " + divY);
944    
945                                  newArea = new Envelope(new Coordinate(newArea.getMinX(),                                  newArea = new Envelope(new Coordinate(newArea.getMinX(),
946                                                  newArea.getMinY() - divY), new Coordinate(newArea                                                  newArea.getMinY() - divY), new Coordinate(newArea
947                                                  .getMaxX(), newArea.getMaxY() - divY));                                                  .getMaxX(), newArea.getMaxY() - divY));
948    
949                                  if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {                                  if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
950                                          LOGGER.debug("Now it exeeds the top border.. cut!");                                          // LOGGER.debug("Now it exeeds the top border.. cut!");
951                                          // And cut the bottom if it moved out of the area                                          // And cut the bottom if it moved out of the area
952                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),
953                                                          newArea.getMinY()), new Coordinate(newArea                                                          newArea.getMinY()), new Coordinate(newArea
954                                                          .getMaxX(), maxAllowedExtend.getMaxY()));                                                          .getMaxX(), maxAllowedExtend.getMaxY()));
955    
956                                          LOGGER.debug("and fix aspect ratio");                                          // LOGGER.debug("and fix aspect ratio");
957    
958                                          newArea = fixAspectRatio(this.getBounds(), newArea, false);                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
959                                                            false);
960                                  }                                  }
961                          }                          }
962    
# Line 1009  public class JMapPane extends JPanel imp Line 965  public class JMapPane extends JPanel imp
965    
966                                  // Move left..                                  // Move left..
967                                  double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();                                  double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
968                                  LOGGER.debug("Moving area left by " + divX);                                  // LOGGER.debug("Moving area left by " + divX);
969    
970                                  newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,                                  newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
971                                                  newArea.getMinY()), new Coordinate(newArea.getMaxX()                                                  newArea.getMinY()), new Coordinate(newArea.getMaxX()
972                                                  - divX, newArea.getMaxY()));                                                  - divX, newArea.getMaxY()));
973    
974                                  if (newArea.getMinX() < maxAllowedExtend.getMinX()) {                                  if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
975                                          LOGGER.debug("Now it exeeds the left border.. cut!");                                          // LOGGER.debug("Now it exeeds the left border.. cut!");
976                                          // And cut the left if it moved out of the area                                          // And cut the left if it moved out of the area
977                                          newArea = new Envelope(new Coordinate(maxAllowedExtend.getMinX(),                                          newArea = new Envelope(new Coordinate(maxAllowedExtend
978                                                          newArea.getMinY()), new Coordinate(newArea                                                          .getMinX(), newArea.getMinY()), new Coordinate(
979                                                          .getMaxX(), newArea.getMaxY()));                                                          newArea.getMaxX(), newArea.getMaxY()));
980    
981                                          LOGGER.debug("and fix aspect ratio");                                          // LOGGER.debug("and fix aspect ratio");
982    
983                                          newArea = fixAspectRatio(this.getBounds(), newArea, false);                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
984                                                            false);
985                                  }                                  }
986                          }                          }
987    
# Line 1033  public class JMapPane extends JPanel imp Line 990  public class JMapPane extends JPanel imp
990    
991                                  // Move right..                                  // Move right..
992                                  double divX = newArea.getMinX() - maxAllowedExtend.getMinX();                                  double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
993                                  LOGGER.debug("Moving area right by " + divX);                                  // LOGGER.debug("Moving area right by " + divX);
994    
995                                  newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,                                  newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
996                                                  newArea.getMinY()), new Coordinate(newArea.getMaxX()                                                  newArea.getMinY()), new Coordinate(newArea.getMaxX()
997                                                  - divX, newArea.getMaxY()));                                                  - divX, newArea.getMaxY()));
998    
999                                  if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {                                  if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
1000                                          LOGGER.debug("Now it exeeds the right border.. cut!");                                          // LOGGER.debug("Now it exeeds the right border.. cut!");
1001                                          // And cut the left if it moved out of the area                                          // And cut the left if it moved out of the area
1002                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),
1003                                                          newArea.getMinY()), new Coordinate(maxAllowedExtend                                                          newArea.getMinY()), new Coordinate(maxAllowedExtend
1004                                                          .getMaxX(), newArea.getMaxY()));                                                          .getMaxX(), newArea.getMaxY()));
1005    
1006                                          LOGGER.debug("and fix aspect ratio");                                          // LOGGER.debug("and fix aspect ratio");
1007    
1008                                          newArea = fixAspectRatio(this.getBounds(), newArea, false);                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea,
1009                                                            false);
1010                                  }                                  }
1011                          }                          }
1012    
# Line 1081  public class JMapPane extends JPanel imp Line 1039  public class JMapPane extends JPanel imp
1039    
1040          /**          /**
1041           * Set the maximum allowed zoom scale. This is the smaller number value of           * Set the maximum allowed zoom scale. This is the smaller number value of
1042           * the two.           * the two. If <code>null</code> is passed, Double.MINVALUE are used which
1043             * mean there is no restriction.
1044           *           *
1045           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
1046           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
1047           */           */
1048          public void setMaxZoomScale(final Double maxZoomScale) {          public void setMaxZoomScale(final Double maxZoomScale) {
1049                  this.maxZoomScale = maxZoomScale;                  this.maxZoomScale = maxZoomScale == null ? Double.MIN_VALUE
1050                                    : maxZoomScale;
1051          }          }
1052    
1053          /**          /**
1054           * Set the minimum (nearest) allowed zoom scale. This is the bigger number           * Set the minimum (nearest) allowed zoom scale. This is the bigger number
1055           * value of the two.           * value of the two. If <code>null</code> is passed, Double.MAXVALUE are
1056             * used which mean there is no restriction.
1057             *
1058           *           *
1059           * @author <a href="mailto:[email protected]">Stefan Alfons           * @author <a href="mailto:[email protected]">Stefan Alfons
1060           *         Kr&uuml;ger</a>           *         Kr&uuml;ger</a>
1061           */           */
1062          public void setMinZoomScale(final Double minZoomScale) {          public void setMinZoomScale(final Double minZoomScale) {
1063                  this.minZoomScale = minZoomScale;                  this.minZoomScale = minZoomScale == null ? Double.MAX_VALUE
1064                                    : minZoomScale;
1065          }          }
1066    
1067          /**          /**
# Line 1126  public class JMapPane extends JPanel imp Line 1089  public class JMapPane extends JPanel imp
1089          public Envelope getMaxExtend() {          public Envelope getMaxExtend() {
1090                  if (maxExtend == null) {                  if (maxExtend == null) {
1091                          try {                          try {
1092                                  return fixAspectRatio(this.getBounds(), context.getLayerBounds(), true);                                  return JTSUtil.fixAspectRatio(this.getBounds(),
1093                                                    // Kartenbereich um 10% vergroessern
1094                                                    JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1),
1095                                                    true);
1096                          } catch (IOException e) {                          } catch (IOException e) {
1097                                  LOGGER                                  LOGGER
1098                                                  .warn(                                                  .warn(
1099                                                                  "maxExtend == null; faild to getLayerBounds of context",                                                                  "maxExtend == null; failed to getLayerBounds of context",
1100                                                                  e);                                                                  e);
1101                          }                          }
1102                  }                  }

Legend:
Removed from v.420  
changed lines
  Added in v.504

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26