58 |
import javax.swing.JPanel; |
import javax.swing.JPanel; |
59 |
|
|
60 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
61 |
|
import org.geotools.geometry.jts.ReferencedEnvelope; |
62 |
import org.geotools.map.MapContext; |
import org.geotools.map.MapContext; |
63 |
import org.geotools.map.event.MapLayerListEvent; |
import org.geotools.map.event.MapLayerListEvent; |
64 |
import org.geotools.map.event.MapLayerListListener; |
import org.geotools.map.event.MapLayerListListener; |
70 |
import org.opengis.filter.FilterFactory2; |
import org.opengis.filter.FilterFactory2; |
71 |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
72 |
|
|
73 |
|
import schmitzm.geotools.JTSUtil; |
74 |
import schmitzm.swing.SwingUtil; |
import schmitzm.swing.SwingUtil; |
75 |
|
|
76 |
import com.vividsolutions.jts.geom.Coordinate; |
import com.vividsolutions.jts.geom.Coordinate; |
335 |
reset = false; /* forget about the reset */ |
reset = false; /* forget about the reset */ |
336 |
oldRect = r; /* store what the current size is */ |
oldRect = r; /* store what the current size is */ |
337 |
|
|
338 |
mapArea = fixAspectRatio(r, mapArea); |
mapArea = fixAspectRatio(r, mapArea, false); |
339 |
} |
} |
340 |
} |
} |
341 |
|
|
371 |
((Graphics2D) g).drawImage(baseImage, 0, 0, this); |
((Graphics2D) g).drawImage(baseImage, 0, 0, this); |
372 |
} |
} |
373 |
|
|
374 |
private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea) { |
/** |
375 |
|
* Returns an {@link Envelope} that has the same aspect ratio as the given rectangle |
376 |
|
* @param grow |
377 |
|
* If <code>true</code>, than the area will be enlarged to match |
378 |
|
* the aspect ratio. If <code>false</code>, it will only shrink. |
379 |
|
*/ |
380 |
|
private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea, |
381 |
|
boolean grow) { |
382 |
|
|
383 |
|
if (mapArea == null) { |
384 |
|
LOGGER.warn("mapArea has been null in method fixAspectRatio, returning an unmodified "); |
385 |
|
} |
386 |
|
|
387 |
final double mapWidth = mapArea.getWidth(); /* get the extent of the map */ |
final double mapWidth = mapArea.getWidth(); /* get the extent of the map */ |
388 |
final double mapHeight = mapArea.getHeight(); |
final double mapHeight = mapArea.getHeight(); |
394 |
final double scaleY = r.getHeight() / mapArea.getHeight(); |
final double scaleY = r.getHeight() / mapArea.getHeight(); |
395 |
double scale = 1.0; // stupid compiler! |
double scale = 1.0; // stupid compiler! |
396 |
|
|
397 |
if (scaleX > scaleY) { /* pick the smaller scale */ |
if ((grow && scaleX < scaleY) || (!grow && scaleX > scaleY)) { |
398 |
scale = scaleX; |
scale = scaleX; |
399 |
} else { |
} else { |
400 |
scale = scaleY; |
scale = scaleY; |
627 |
protected void processDrag(final int x1, final int y1, final int x2, |
protected void processDrag(final int x1, final int y1, final int x2, |
628 |
final int y2, final MouseEvent e) { |
final int y2, final MouseEvent e) { |
629 |
|
|
630 |
|
|
631 |
/**** |
/**** |
632 |
* If no layer is availabe we dont want a NullPointerException |
* If no layers exist, we ignore the drag. |
633 |
*/ |
*/ |
634 |
if (mapArea == null) |
if (context.getLayerCount() <= 0) { |
635 |
return; |
return; |
636 |
|
} |
637 |
|
|
638 |
// System.out.println("processing drag from " + x1 + "," + y1 + " -> " |
// System.out.println("processing drag from " + x1 + "," + y1 + " -> " |
639 |
// + x2 + "," + y2); |
// + x2 + "," + y2); |
640 |
if ((x1 == x2) && (y1 == y2)) { |
if ((x1 == x2) && (y1 == y2)) { |
680 |
final Coordinate ur = new Coordinate(right, top); |
final Coordinate ur = new Coordinate(right, top); |
681 |
// xulu.sc |
// xulu.sc |
682 |
// mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur)); |
// mapArea = fixAspectRatio(this.getBounds(), new Envelope(ll, ur)); |
683 |
setMapArea(fixAspectRatio(this.getBounds(), new Envelope(ll, ur))); |
|
684 |
|
|
685 |
|
setMapArea(bestAllowedMapArea(new Envelope(ll, ur))); |
686 |
// xulu.ec |
// xulu.ec |
687 |
} else if (state == JMapPane.ZoomIn) { |
} else if (state == JMapPane.ZoomIn) { |
688 |
|
|
928 |
* Correct the aspect Ratio before we check the rest. Otherwise we might |
* Correct the aspect Ratio before we check the rest. Otherwise we might |
929 |
* easily fail. |
* easily fail. |
930 |
*/ |
*/ |
931 |
env = fixAspectRatio(this.getBounds(), env); |
env = fixAspectRatio(this.getBounds(), env, false); |
932 |
|
|
933 |
final double scale = env.getWidth() / getWidth(); |
final double scale = env.getWidth() / getWidth(); |
934 |
final double centerX = env.getMinX() + env.getWidth() / 2.; |
final double centerX = env.getMinX() + env.getWidth() / 2.; |
964 |
newArea = new Envelope(ll, ur); |
newArea = new Envelope(ll, ur); |
965 |
} |
} |
966 |
|
|
967 |
if (getMaxExtend() != null) { |
Envelope maxAllowedExtend = getMaxExtend(); |
968 |
|
while (maxAllowedExtend != null && !maxAllowedExtend.contains(newArea)) { |
969 |
while (!getMaxExtend().contains(newArea)) { |
/* |
970 |
/* |
* If a maxExtend is set, we have to honour that... |
971 |
* 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); |
|
972 |
|
|
973 |
|
// Exceeds top? Move down and maybe cut |
974 |
|
if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) { |
975 |
|
double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY(); |
976 |
|
// LOGGER.debug("Moving area down by " + divY); |
977 |
|
|
978 |
|
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
979 |
|
newArea.getMinY() - divY), new Coordinate(newArea |
980 |
|
.getMaxX(), newArea.getMaxY() - divY)); |
981 |
|
|
982 |
|
if (newArea.getMinY() < maxAllowedExtend.getMinY()) { |
983 |
|
// LOGGER.debug("Now it exeeds the bottom border.. cut!"); |
984 |
|
// And cut the bottom if it moved out of the area |
985 |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
986 |
newArea.getMinY() - divY), new Coordinate(newArea |
maxAllowedExtend.getMinY()), new Coordinate(newArea |
987 |
.getMaxX(), newArea.getMaxY() - divY)); |
.getMaxX(), newArea.getMaxY())); |
|
|
|
|
if (newArea.getMinY() < maxExtend.getMinY()) { |
|
|
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())); |
|
988 |
|
|
989 |
LOGGER.debug("and fix aspect ratio"); |
// LOGGER.debug("and fix aspect ratio"); |
990 |
|
|
991 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
992 |
} |
} |
993 |
|
} |
994 |
|
|
995 |
// Exceeds bottom? Move up and maybe cut |
// Exceeds bottom? Move up and maybe cut |
996 |
if (newArea.getMinY() < maxExtend.getMinY()) { |
if (newArea.getMinY() < maxAllowedExtend.getMinY()) { |
997 |
double divY = newArea.getMinY() - maxExtend.getMinY(); |
double divY = newArea.getMinY() - maxAllowedExtend.getMinY(); |
998 |
LOGGER.debug("Moving area up by " + divY); |
// LOGGER.debug("Moving area up by " + divY); |
999 |
|
|
1000 |
|
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
1001 |
|
newArea.getMinY() - divY), new Coordinate(newArea |
1002 |
|
.getMaxX(), newArea.getMaxY() - divY)); |
1003 |
|
|
1004 |
|
if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) { |
1005 |
|
LOGGER.debug("Now it exeeds the top border.. cut!"); |
1006 |
|
// And cut the bottom if it moved out of the area |
1007 |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
1008 |
newArea.getMinY() - divY), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea |
1009 |
.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())); |
|
1010 |
|
|
1011 |
LOGGER.debug("and fix aspect ratio"); |
LOGGER.debug("and fix aspect ratio"); |
1012 |
|
|
1013 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
1014 |
} |
} |
1015 |
|
} |
1016 |
|
|
1017 |
// Exceeds to the right? move and maybe cut |
// Exceeds to the right? move and maybe cut |
1018 |
if (newArea.getMaxX() > maxExtend.getMaxX()) { |
if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) { |
1019 |
|
|
1020 |
// Move left.. |
// Move left.. |
1021 |
double divX = newArea.getMaxX() - maxExtend.getMaxX(); |
double divX = newArea.getMaxX() - maxAllowedExtend.getMaxX(); |
1022 |
LOGGER.debug("Moving area left by " + divX); |
// LOGGER.debug("Moving area left by " + divX); |
1023 |
|
|
1024 |
newArea = new Envelope(new Coordinate(newArea.getMinX() |
newArea = new Envelope(new Coordinate(newArea.getMinX() - divX, |
1025 |
- divX, newArea.getMinY()), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea.getMaxX() |
1026 |
.getMaxX() |
- divX, newArea.getMaxY())); |
1027 |
- divX, newArea.getMaxY())); |
|
1028 |
|
if (newArea.getMinX() < maxAllowedExtend.getMinX()) { |
1029 |
if (newArea.getMinX() < maxExtend.getMinX()) { |
LOGGER.debug("Now it exeeds the left border.. cut!"); |
1030 |
LOGGER.debug("Now it exeeds the left border.. cut!"); |
// And cut the left if it moved out of the area |
1031 |
// And cut the left if it moved out of the area |
newArea = new Envelope(new Coordinate(maxAllowedExtend.getMinX(), |
1032 |
newArea = new Envelope(new Coordinate(maxExtend |
newArea.getMinY()), new Coordinate(newArea |
1033 |
.getMinX(), newArea.getMinY()), new Coordinate( |
.getMaxX(), newArea.getMaxY())); |
|
newArea.getMaxX(), newArea.getMaxY())); |
|
1034 |
|
|
1035 |
LOGGER.debug("and fix aspect ratio"); |
LOGGER.debug("and fix aspect ratio"); |
1036 |
|
|
1037 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
1038 |
} |
} |
1039 |
|
} |
1040 |
|
|
1041 |
// Exceeds to the left? move and maybe cut |
// Exceeds to the left? move and maybe cut |
1042 |
if (newArea.getMinX() < maxExtend.getMinX()) { |
if (newArea.getMinX() < maxAllowedExtend.getMinX()) { |
1043 |
|
|
1044 |
// Move right.. |
// Move right.. |
1045 |
double divX = newArea.getMinX() - maxExtend.getMinX(); |
double divX = newArea.getMinX() - maxAllowedExtend.getMinX(); |
1046 |
LOGGER.debug("Moving area right by " + divX); |
// LOGGER.debug("Moving area right by " + divX); |
1047 |
|
|
1048 |
newArea = new Envelope(new Coordinate(newArea.getMinX() |
newArea = new Envelope(new Coordinate(newArea.getMinX() - divX, |
1049 |
- divX, newArea.getMinY()), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea.getMaxX() |
1050 |
.getMaxX() |
- divX, newArea.getMaxY())); |
1051 |
- divX, newArea.getMaxY())); |
|
1052 |
|
if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) { |
1053 |
if (newArea.getMaxX() > maxExtend.getMaxX()) { |
LOGGER.debug("Now it exeeds the right border.. cut!"); |
1054 |
LOGGER.debug("Now it exeeds the right border.. cut!"); |
// And cut the left if it moved out of the area |
1055 |
// And cut the left if it moved out of the area |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
1056 |
newArea = new Envelope(new Coordinate( |
newArea.getMinY()), new Coordinate(maxAllowedExtend |
1057 |
newArea.getMinX(), newArea.getMinY()), |
.getMaxX(), newArea.getMaxY())); |
|
new Coordinate(maxExtend.getMaxX(), newArea |
|
|
.getMaxY())); |
|
1058 |
|
|
1059 |
LOGGER.debug("and fix aspect ratio"); |
LOGGER.debug("and fix aspect ratio"); |
1060 |
|
|
1061 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
1062 |
} |
} |
|
|
|
1063 |
} |
} |
1064 |
|
|
1065 |
} |
} |
1124 |
|
|
1125 |
/** |
/** |
1126 |
* Returns the evelope of the viewable area. The JMapPane will never show |
* Returns the evelope of the viewable area. The JMapPane will never show |
1127 |
* anything outside of this extend. |
* anything outside of this extend. If this has been set to |
1128 |
|
* <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return |
1129 |
|
* quickly the context's bounds. It it takes to long to determine the |
1130 |
|
* context bounds, <code>null</code> is returned. |
1131 |
* |
* |
1132 |
* @param maxExtend |
* @param maxExtend |
1133 |
* <code>null</code> to not have this restriction. |
* <code>null</code> to not have this restriction. |
1134 |
*/ |
*/ |
1135 |
|
|
1136 |
public Envelope getMaxExtend() { |
public Envelope getMaxExtend() { |
1137 |
|
if (maxExtend == null) { |
1138 |
|
try { |
1139 |
|
return fixAspectRatio( |
1140 |
|
this.getBounds(), |
1141 |
|
// Kartenbereich um 10% vergroessern |
1142 |
|
JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1), |
1143 |
|
true |
1144 |
|
); |
1145 |
|
} catch (IOException e) { |
1146 |
|
LOGGER.warn( |
1147 |
|
"maxExtend == null; faild to getLayerBounds of context", |
1148 |
|
e); |
1149 |
|
} |
1150 |
|
} |
1151 |
return maxExtend; |
return maxExtend; |
1152 |
} |
} |
1153 |
|
|