58 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
59 |
|
|
60 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
|
import org.geotools.geometry.jts.ReferencedEnvelope; |
|
61 |
import org.geotools.map.MapContext; |
import org.geotools.map.MapContext; |
62 |
import org.geotools.map.event.MapLayerListEvent; |
import org.geotools.map.event.MapLayerListEvent; |
63 |
import org.geotools.map.event.MapLayerListListener; |
import org.geotools.map.event.MapLayerListListener; |
333 |
reset = false; /* forget about the reset */ |
reset = false; /* forget about the reset */ |
334 |
oldRect = r; /* store what the current size is */ |
oldRect = r; /* store what the current size is */ |
335 |
|
|
336 |
mapArea = fixAspectRatio(r, mapArea); |
mapArea = fixAspectRatio(r, mapArea, false); |
337 |
} |
} |
338 |
} |
} |
339 |
|
|
369 |
((Graphics2D) g).drawImage(baseImage, 0, 0, this); |
((Graphics2D) g).drawImage(baseImage, 0, 0, this); |
370 |
} |
} |
371 |
|
|
372 |
private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea) { |
/** |
373 |
|
* Returns an {@link Envelope} that has the same aspect ratio as the given rectangle |
374 |
|
* @param grow |
375 |
|
* If <code>true</code>, than the area will be enlarged to match |
376 |
|
* the aspect ratio. If <code>false</code>, it will only shrink. |
377 |
|
*/ |
378 |
|
private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea, |
379 |
|
boolean grow) { |
380 |
|
|
381 |
final double mapWidth = mapArea.getWidth(); /* get the extent of the map */ |
final double mapWidth = mapArea.getWidth(); /* get the extent of the map */ |
382 |
final double mapHeight = mapArea.getHeight(); |
final double mapHeight = mapArea.getHeight(); |
388 |
final double scaleY = r.getHeight() / mapArea.getHeight(); |
final double scaleY = r.getHeight() / mapArea.getHeight(); |
389 |
double scale = 1.0; // stupid compiler! |
double scale = 1.0; // stupid compiler! |
390 |
|
|
391 |
if (scaleX > scaleY) { /* pick the smaller scale */ |
if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) { |
392 |
scale = scaleX; |
scale = scaleX; |
393 |
} else { |
} else { |
394 |
scale = scaleY; |
scale = scaleY; |
672 |
final Coordinate ur = new Coordinate(right, top); |
final Coordinate ur = new Coordinate(right, top); |
673 |
// xulu.sc |
// xulu.sc |
674 |
// mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur)); |
// mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur)); |
675 |
setMapArea(fixAspectRatio(this.getBounds(), new Envelope(ll, ur))); |
setMapArea(bestAllowedMapArea(new Envelope(ll, ur))); |
676 |
// xulu.ec |
// xulu.ec |
677 |
} else if (state == JMapPane.ZoomIn) { |
} else if (state == JMapPane.ZoomIn) { |
678 |
|
|
918 |
* Correct the aspect Ratio before we check the rest. Otherwise we might |
* Correct the aspect Ratio before we check the rest. Otherwise we might |
919 |
* easily fail. |
* easily fail. |
920 |
*/ |
*/ |
921 |
env = fixAspectRatio(this.getBounds(), env); |
env = fixAspectRatio(this.getBounds(), env, false); |
922 |
|
|
923 |
final double scale = env.getWidth() / getWidth(); |
final double scale = env.getWidth() / getWidth(); |
924 |
final double centerX = env.getMinX() + env.getWidth() / 2.; |
final double centerX = env.getMinX() + env.getWidth() / 2.; |
954 |
newArea = new Envelope(ll, ur); |
newArea = new Envelope(ll, ur); |
955 |
} |
} |
956 |
|
|
957 |
if (getMaxExtend() != null) { |
Envelope maxAllowedExtend = getMaxExtend(); |
958 |
|
while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)) { |
959 |
while (!getMaxExtend().contains(newArea)) { |
/* |
960 |
/* |
* If a maxExtend is set, we have to honour that... |
961 |
* If a maxExtend is set, we have to honour that... |
*/ |
|
*/ |
|
|
|
|
|
// Exceeds top? Move down and maybe cut |
|
|
if (newArea.getMaxY() > maxExtend.getMaxY()) { |
|
|
double divY = newArea.getMaxY() - maxExtend.getMaxY(); |
|
|
LOGGER.debug("Moving area down by " + divY); |
|
962 |
|
|
963 |
|
// Exceeds top? Move down and maybe cut |
964 |
|
if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) { |
965 |
|
double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY(); |
966 |
|
LOGGER.debug("Moving area down by " + divY); |
967 |
|
|
968 |
|
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
969 |
|
newArea.getMinY() - divY), new Coordinate(newArea |
970 |
|
.getMaxX(), newArea.getMaxY() - divY)); |
971 |
|
|
972 |
|
if (newArea.getMinY() < maxAllowedExtend.getMinY()) { |
973 |
|
LOGGER.debug("Now it exeeds the bottom border.. cut!"); |
974 |
|
// And cut the bottom if it moved out of the area |
975 |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
976 |
newArea.getMinY() - divY), new Coordinate(newArea |
maxAllowedExtend.getMinY()), new Coordinate(newArea |
977 |
.getMaxX(), newArea.getMaxY() - divY)); |
.getMaxX(), newArea.getMaxY())); |
978 |
|
|
979 |
if (newArea.getMinY() < maxExtend.getMinY()) { |
LOGGER.debug("and fix aspect ratio"); |
|
LOGGER.debug("Now it exeeds the bottom border.. cut!"); |
|
|
// And cut the bottom if it moved out of the area |
|
|
newArea = new Envelope(new Coordinate( |
|
|
newArea.getMinX(), maxExtend.getMinY()), |
|
|
new Coordinate(newArea.getMaxX(), newArea |
|
|
.getMaxY())); |
|
980 |
|
|
981 |
LOGGER.debug("and fix aspect ratio"); |
newArea = fixAspectRatio(this.getBounds(), newArea, false); |
|
|
|
|
newArea = fixAspectRatio(this.getBounds(), newArea); |
|
|
} |
|
982 |
} |
} |
983 |
|
} |
984 |
|
|
985 |
// Exceeds bottom? Move up and maybe cut |
// Exceeds bottom? Move up and maybe cut |
986 |
if (newArea.getMinY() < maxExtend.getMinY()) { |
if (newArea.getMinY() < maxAllowedExtend.getMinY()) { |
987 |
double divY = newArea.getMinY() - maxExtend.getMinY(); |
double divY = newArea.getMinY() - maxAllowedExtend.getMinY(); |
988 |
LOGGER.debug("Moving area up by " + divY); |
LOGGER.debug("Moving area up by " + divY); |
989 |
|
|
990 |
|
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
991 |
|
newArea.getMinY() - divY), new Coordinate(newArea |
992 |
|
.getMaxX(), newArea.getMaxY() - divY)); |
993 |
|
|
994 |
|
if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) { |
995 |
|
LOGGER.debug("Now it exeeds the top border.. cut!"); |
996 |
|
// And cut the bottom if it moved out of the area |
997 |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
998 |
newArea.getMinY() - divY), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea |
999 |
.getMaxX(), newArea.getMaxY() - divY)); |
.getMaxX(), maxAllowedExtend.getMaxY())); |
|
|
|
|
if (newArea.getMaxY() > maxExtend.getMaxY()) { |
|
|
LOGGER.debug("Now it exeeds the top border.. cut!"); |
|
|
// And cut the bottom if it moved out of the area |
|
|
newArea = new Envelope(new Coordinate( |
|
|
newArea.getMinX(), newArea.getMinY()), |
|
|
new Coordinate(newArea.getMaxX(), maxExtend |
|
|
.getMaxY())); |
|
1000 |
|
|
1001 |
LOGGER.debug("and fix aspect ratio"); |
LOGGER.debug("and fix aspect ratio"); |
1002 |
|
|
1003 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
1004 |
} |
} |
1005 |
|
} |
1006 |
|
|
1007 |
// Exceeds to the right? move and maybe cut |
// Exceeds to the right? move and maybe cut |
1008 |
if (newArea.getMaxX() > maxExtend.getMaxX()) { |
if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) { |
1009 |
|
|
1010 |
// Move left.. |
// Move left.. |
1011 |
double divX = newArea.getMaxX() - maxExtend.getMaxX(); |
double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX(); |
1012 |
LOGGER.debug("Moving area left by " + divX); |
LOGGER.debug("Moving area left by " + divX); |
1013 |
|
|
1014 |
newArea = new Envelope(new Coordinate(newArea.getMinX() |
newArea = new Envelope(new Coordinate(newArea.getMinX() - divX, |
1015 |
- divX, newArea.getMinY()), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea.getMaxX() |
1016 |
.getMaxX() |
- divX, newArea.getMaxY())); |
1017 |
- divX, newArea.getMaxY())); |
|
1018 |
|
if (newArea.getMinX() < maxAllowedExtend.getMinX()) { |
1019 |
if (newArea.getMinX() < maxExtend.getMinX()) { |
LOGGER.debug("Now it exeeds the left border.. cut!"); |
1020 |
LOGGER.debug("Now it exeeds the left border.. cut!"); |
// And cut the left if it moved out of the area |
1021 |
// And cut the left if it moved out of the area |
newArea = new Envelope(new Coordinate(maxExtend.getMinX(), |
1022 |
newArea = new Envelope(new Coordinate(maxExtend |
newArea.getMinY()), new Coordinate(newArea |
1023 |
.getMinX(), newArea.getMinY()), new Coordinate( |
.getMaxX(), newArea.getMaxY())); |
|
newArea.getMaxX(), newArea.getMaxY())); |
|
1024 |
|
|
1025 |
LOGGER.debug("and fix aspect ratio"); |
LOGGER.debug("and fix aspect ratio"); |
1026 |
|
|
1027 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
1028 |
} |
} |
1029 |
|
} |
1030 |
|
|
1031 |
// Exceeds to the left? move and maybe cut |
// Exceeds to the left? move and maybe cut |
1032 |
if (newArea.getMinX() < maxExtend.getMinX()) { |
if (newArea.getMinX() < maxAllowedExtend.getMinX()) { |
1033 |
|
|
1034 |
// Move right.. |
// Move right.. |
1035 |
double divX = newArea.getMinX() - maxExtend.getMinX(); |
double divX = newArea.getMinX() - maxAllowedExtend.getMinX(); |
1036 |
LOGGER.debug("Moving area right by " + divX); |
LOGGER.debug("Moving area right by " + divX); |
1037 |
|
|
1038 |
newArea = new Envelope(new Coordinate(newArea.getMinX() |
newArea = new Envelope(new Coordinate(newArea.getMinX() - divX, |
1039 |
- divX, newArea.getMinY()), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea.getMaxX() |
1040 |
.getMaxX() |
- divX, newArea.getMaxY())); |
1041 |
- divX, newArea.getMaxY())); |
|
1042 |
|
if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) { |
1043 |
if (newArea.getMaxX() > maxExtend.getMaxX()) { |
LOGGER.debug("Now it exeeds the right border.. cut!"); |
1044 |
LOGGER.debug("Now it exeeds the right border.. cut!"); |
// And cut the left if it moved out of the area |
1045 |
// And cut the left if it moved out of the area |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
1046 |
newArea = new Envelope(new Coordinate( |
newArea.getMinY()), new Coordinate(maxAllowedExtend |
1047 |
newArea.getMinX(), newArea.getMinY()), |
.getMaxX(), newArea.getMaxY())); |
|
new Coordinate(maxExtend.getMaxX(), newArea |
|
|
.getMaxY())); |
|
1048 |
|
|
1049 |
LOGGER.debug("and fix aspect ratio"); |
LOGGER.debug("and fix aspect ratio"); |
1050 |
|
|
1051 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
1052 |
} |
} |
|
|
|
1053 |
} |
} |
1054 |
|
|
1055 |
} |
} |
1114 |
|
|
1115 |
/** |
/** |
1116 |
* Returns the evelope of the viewable area. The JMapPane will never show |
* Returns the evelope of the viewable area. The JMapPane will never show |
1117 |
* anything outside of this extend. |
* anything outside of this extend. If this has been set to |
1118 |
|
* <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return |
1119 |
|
* quickly the context's bounds. It it takes to long to determine the |
1120 |
|
* context bounds, <code>null</code> is returned. |
1121 |
* |
* |
1122 |
* @param maxExtend |
* @param maxExtend |
1123 |
* <code>null</code> to not have this restriction. |
* <code>null</code> to not have this restriction. |
1124 |
*/ |
*/ |
1125 |
|
|
1126 |
public Envelope getMaxExtend() { |
public Envelope getMaxExtend() { |
1127 |
|
if (maxExtend == null) { |
1128 |
|
try { |
1129 |
|
return fixAspectRatio(this.getBounds(), context.getLayerBounds(), true); |
1130 |
|
} catch (IOException e) { |
1131 |
|
LOGGER |
1132 |
|
.warn( |
1133 |
|
"maxExtend == null; faild to getLayerBounds of context", |
1134 |
|
e); |
1135 |
|
} |
1136 |
|
} |
1137 |
return maxExtend; |
return maxExtend; |
1138 |
} |
} |
1139 |
|
|