/[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 418 by alfonx, Mon Sep 28 15:36:11 2009 UTC revision 495 by mojays, Fri Oct 23 14:19:46 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) {
                                 || renderer instanceof ShapefileRenderer) {  
235                          hints = renderer.getRendererHints();                          hints = renderer.getRendererHints();
236                          if (hints == null) {                          if (hints == null) {
237                                  hints = new HashMap<Object, Object>();                                  hints = new HashMap<Object, Object>();
# Line 247  public class JMapPane extends JPanel imp Line 246  public class JMapPane extends JPanel imp
246                          hints.put("memoryPreloadingEnabled", Boolean.TRUE);                          hints.put("memoryPreloadingEnabled", Boolean.TRUE);
247    
248                          renderer.setRendererHints(hints);                          renderer.setRendererHints(hints);
249                  }  //              }
   
                 // this.highlightRenderer = new StreamingRenderer();  
                 // this.selectionRenderer = new StreamingRenderer();  
   
                 // highlightRenderer.setRendererHints(hints);  
                 // selectionRenderer.setRendererHints(hints);  
   
                 // renderer.setRendererHints(hints);  
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 621  public class JMapPane extends JPanel imp Line 567  public class JMapPane extends JPanel imp
567          protected void processDrag(final int x1, final int y1, final int x2,          protected void processDrag(final int x1, final int y1, final int x2,
568                          final int y2, final MouseEvent e) {                          final int y2, final MouseEvent e) {
569    
570    
571                  /****                  /****
572                   * If no layer is availabe we dont want a NullPointerException                   * If no layers exist, we ignore the drag.
573                   */                   */
574                  if (mapArea == null)                  if (context.getLayerCount() <= 0) {
575                          return;                          return;
576                    }
577                    
578                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "                  // System.out.println("processing drag from " + x1 + "," + y1 + " -> "
579                  // + x2 + "," + y2);                  // + x2 + "," + y2);
580                  if ((x1 == x2) && (y1 == y2)) {                  if ((x1 == x2) && (y1 == y2)) {
# Line 672  public class JMapPane extends JPanel imp Line 620  public class JMapPane extends JPanel imp
620                          final Coordinate ur = new Coordinate(right, top);                          final Coordinate ur = new Coordinate(right, top);
621                          // xulu.sc                          // xulu.sc
622                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));                          // mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur));
623                            
624    
625                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));                          setMapArea(bestAllowedMapArea(new Envelope(ll, ur)));
626                          // xulu.ec                          // xulu.ec
627                  } else if (state == JMapPane.ZoomIn) {                  } else if (state == JMapPane.ZoomIn) {
# Line 918  public class JMapPane extends JPanel imp Line 868  public class JMapPane extends JPanel imp
868                   * Correct the aspect Ratio before we check the rest. Otherwise we might                   * Correct the aspect Ratio before we check the rest. Otherwise we might
869                   * easily fail.                   * easily fail.
870                   */                   */
871                  env = fixAspectRatio(this.getBounds(), env, false);                  env = JTSUtil.fixAspectRatio(this.getBounds(), env, false);
872    
873                  final double scale = env.getWidth() / getWidth();                  final double scale = env.getWidth() / getWidth();
874                  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 905  public class JMapPane extends JPanel imp
905                  }                  }
906    
907                  Envelope maxAllowedExtend = getMaxExtend();                  Envelope maxAllowedExtend = getMaxExtend();
908                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)) {                  while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea) && newArea != null && !newArea.isNull() && !Double.isNaN(newArea.getMinX()) && !Double.isNaN(newArea.getMaxX()) && !Double.isNaN(newArea.getMinY()) && !Double.isNaN(newArea.getMaxY()) ) {
909                          /*                          /*
910                           * If a maxExtend is set, we have to honour that...                           * If a maxExtend is set, we have to honour that...
911                           */                           */
# Line 963  public class JMapPane extends JPanel imp Line 913  public class JMapPane extends JPanel imp
913                          // Exceeds top? Move down and maybe cut                          // Exceeds top? Move down and maybe cut
914                          if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {                          if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
915                                  double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();                                  double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY();
916                                  LOGGER.debug("Moving area down by " + divY);  //                              LOGGER.debug("Moving area down by " + divY);
917    
918                                  newArea = new Envelope(new Coordinate(newArea.getMinX(),                                  newArea = new Envelope(new Coordinate(newArea.getMinX(),
919                                                  newArea.getMinY() - divY), new Coordinate(newArea                                                  newArea.getMinY() - divY), new Coordinate(newArea
920                                                  .getMaxX(), newArea.getMaxY() - divY));                                                  .getMaxX(), newArea.getMaxY() - divY));
921    
922                                  if (newArea.getMinY() < maxAllowedExtend.getMinY()) {                                  if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
923                                          LOGGER.debug("Now it exeeds the bottom border.. cut!");  //                                      LOGGER.debug("Now it exeeds the bottom border.. cut!");
924                                          // And cut the bottom if it moved out of the area                                          // And cut the bottom if it moved out of the area
925                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),
926                                                          maxAllowedExtend.getMinY()), new Coordinate(newArea                                                          maxAllowedExtend.getMinY()), new Coordinate(newArea
927                                                          .getMaxX(), newArea.getMaxY()));                                                          .getMaxX(), newArea.getMaxY()));
928    
929                                          LOGGER.debug("and fix aspect ratio");  //                                      LOGGER.debug("and fix aspect ratio");
930    
931                                          newArea = fixAspectRatio(this.getBounds(), newArea, false);                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false);
932                                  }                                  }
933                          }                          }
934    
935                          // Exceeds bottom? Move up and maybe cut                          // Exceeds bottom? Move up and maybe cut
936                          if (newArea.getMinY() < maxAllowedExtend.getMinY()) {                          if (newArea.getMinY() < maxAllowedExtend.getMinY()) {
937                                  double divY = newArea.getMinY() - maxAllowedExtend.getMinY();                                  double divY = newArea.getMinY() - maxAllowedExtend.getMinY();
938                                  LOGGER.debug("Moving area up by " + divY);  //                              LOGGER.debug("Moving area up by " + divY);
939    
940                                  newArea = new Envelope(new Coordinate(newArea.getMinX(),                                  newArea = new Envelope(new Coordinate(newArea.getMinX(),
941                                                  newArea.getMinY() - divY), new Coordinate(newArea                                                  newArea.getMinY() - divY), new Coordinate(newArea
942                                                  .getMaxX(), newArea.getMaxY() - divY));                                                  .getMaxX(), newArea.getMaxY() - divY));
943    
944                                  if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {                                  if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) {
945                                          LOGGER.debug("Now it exeeds the top border.. cut!");  //                                      LOGGER.debug("Now it exeeds the top border.. cut!");
946                                          // And cut the bottom if it moved out of the area                                          // And cut the bottom if it moved out of the area
947                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),
948                                                          newArea.getMinY()), new Coordinate(newArea                                                          newArea.getMinY()), new Coordinate(newArea
949                                                          .getMaxX(), maxAllowedExtend.getMaxY()));                                                          .getMaxX(), maxAllowedExtend.getMaxY()));
950    
951                                          LOGGER.debug("and fix aspect ratio");  //                                      LOGGER.debug("and fix aspect ratio");
952    
953                                          newArea = fixAspectRatio(this.getBounds(), newArea, false);                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false);
954                                  }                                  }
955                          }                          }
956    
# Line 1009  public class JMapPane extends JPanel imp Line 959  public class JMapPane extends JPanel imp
959    
960                                  // Move left..                                  // Move left..
961                                  double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();                                  double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX();
962                                  LOGGER.debug("Moving area left by " + divX);  //                              LOGGER.debug("Moving area left by " + divX);
963    
964                                  newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,                                  newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
965                                                  newArea.getMinY()), new Coordinate(newArea.getMaxX()                                                  newArea.getMinY()), new Coordinate(newArea.getMaxX()
966                                                  - divX, newArea.getMaxY()));                                                  - divX, newArea.getMaxY()));
967    
968                                  if (newArea.getMinX() < maxAllowedExtend.getMinX()) {                                  if (newArea.getMinX() < maxAllowedExtend.getMinX()) {
969                                          LOGGER.debug("Now it exeeds the left border.. cut!");  //                                      LOGGER.debug("Now it exeeds the left border.. cut!");
970                                          // And cut the left if it moved out of the area                                          // And cut the left if it moved out of the area
971                                          newArea = new Envelope(new Coordinate(maxExtend.getMinX(),                                          newArea = new Envelope(new Coordinate(maxAllowedExtend.getMinX(),
972                                                          newArea.getMinY()), new Coordinate(newArea                                                          newArea.getMinY()), new Coordinate(newArea
973                                                          .getMaxX(), newArea.getMaxY()));                                                          .getMaxX(), newArea.getMaxY()));
974    
975                                          LOGGER.debug("and fix aspect ratio");  //                                      LOGGER.debug("and fix aspect ratio");
976    
977                                          newArea = fixAspectRatio(this.getBounds(), newArea, false);                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false);
978                                  }                                  }
979                          }                          }
980    
# Line 1033  public class JMapPane extends JPanel imp Line 983  public class JMapPane extends JPanel imp
983    
984                                  // Move right..                                  // Move right..
985                                  double divX = newArea.getMinX() - maxAllowedExtend.getMinX();                                  double divX = newArea.getMinX() - maxAllowedExtend.getMinX();
986                                  LOGGER.debug("Moving area right by " + divX);  //                              LOGGER.debug("Moving area right by " + divX);
987    
988                                  newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,                                  newArea = new Envelope(new Coordinate(newArea.getMinX() - divX,
989                                                  newArea.getMinY()), new Coordinate(newArea.getMaxX()                                                  newArea.getMinY()), new Coordinate(newArea.getMaxX()
990                                                  - divX, newArea.getMaxY()));                                                  - divX, newArea.getMaxY()));
991    
992                                  if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {                                  if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) {
993                                          LOGGER.debug("Now it exeeds the right border.. cut!");  //                                      LOGGER.debug("Now it exeeds the right border.. cut!");
994                                          // And cut the left if it moved out of the area                                          // And cut the left if it moved out of the area
995                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),                                          newArea = new Envelope(new Coordinate(newArea.getMinX(),
996                                                          newArea.getMinY()), new Coordinate(maxAllowedExtend                                                          newArea.getMinY()), new Coordinate(maxAllowedExtend
997                                                          .getMaxX(), newArea.getMaxY()));                                                          .getMaxX(), newArea.getMaxY()));
998    
999                                          LOGGER.debug("and fix aspect ratio");  //                                      LOGGER.debug("and fix aspect ratio");
1000    
1001                                          newArea = fixAspectRatio(this.getBounds(), newArea, false);                                          newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false);
1002                                  }                                  }
1003                          }                          }
1004    
# Line 1126  public class JMapPane extends JPanel imp Line 1076  public class JMapPane extends JPanel imp
1076          public Envelope getMaxExtend() {          public Envelope getMaxExtend() {
1077                  if (maxExtend == null) {                  if (maxExtend == null) {
1078                          try {                          try {
1079                                  return fixAspectRatio(this.getBounds(), context.getLayerBounds(), true);                                  return JTSUtil.fixAspectRatio(
1080                                        this.getBounds(),
1081                            // Kartenbereich um 10% vergroessern
1082                                        JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1),
1083                                        true
1084                                    );
1085                          } catch (IOException e) {                          } catch (IOException e) {
1086                                  LOGGER                                  LOGGER.warn(
1087                                                  .warn(                                                                  "maxExtend == null; failed to getLayerBounds of context",
                                                                 "maxExtend == null; faild to getLayerBounds of context",  
1088                                                                  e);                                                                  e);
1089                          }                          }
1090                  }                  }

Legend:
Removed from v.418  
changed lines
  Added in v.495

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26