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; |
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; |
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 |
|
|
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>(); |
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); |
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); |
mapArea = JTSUtil.fixAspectRatio(r, mapArea, false); |
328 |
} |
} |
329 |
} |
} |
330 |
|
|
360 |
((Graphics2D) g).drawImage(baseImage, 0, 0, this); |
((Graphics2D) g).drawImage(baseImage, 0, 0, this); |
361 |
} |
} |
362 |
|
|
|
private Envelope fixAspectRatio(final Rectangle r, final Envelope mapArea) { |
|
|
|
|
|
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 (scaleX > scaleY) { /* pick the smaller scale */ |
|
|
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; |
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)) { |
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 |
setMapArea(fixAspectRatio(this.getBounds(), new Envelope(ll, ur))); |
|
624 |
|
|
625 |
|
setMapArea(bestAllowedMapArea(new Envelope(ll, ur))); |
626 |
// xulu.ec |
// xulu.ec |
627 |
} else if (state == JMapPane.ZoomIn) { |
} else if (state == JMapPane.ZoomIn) { |
628 |
|
|
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); |
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.; |
904 |
newArea = new Envelope(ll, ur); |
newArea = new Envelope(ll, ur); |
905 |
} |
} |
906 |
|
|
907 |
if (getMaxExtend() != null) { |
Envelope maxAllowedExtend = getMaxExtend(); |
908 |
|
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 |
while (!getMaxExtend().contains(newArea)) { |
/* |
910 |
/* |
* If a maxExtend is set, we have to honour that... |
911 |
* 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); |
|
912 |
|
|
913 |
|
// Exceeds top? Move down and maybe cut |
914 |
|
if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) { |
915 |
|
double divY = newArea.getMaxY() - maxAllowedExtend.getMaxY(); |
916 |
|
// LOGGER.debug("Moving area down by " + divY); |
917 |
|
|
918 |
|
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
919 |
|
newArea.getMinY() - divY), new Coordinate(newArea |
920 |
|
.getMaxX(), newArea.getMaxY() - divY)); |
921 |
|
|
922 |
|
if (newArea.getMinY() < maxAllowedExtend.getMinY()) { |
923 |
|
// LOGGER.debug("Now it exeeds the bottom border.. cut!"); |
924 |
|
// 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 |
newArea.getMinY() - divY), new Coordinate(newArea |
maxAllowedExtend.getMinY()), new Coordinate(newArea |
927 |
.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())); |
|
928 |
|
|
929 |
LOGGER.debug("and fix aspect ratio"); |
// LOGGER.debug("and fix aspect ratio"); |
930 |
|
|
931 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
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() < maxExtend.getMinY()) { |
if (newArea.getMinY() < maxAllowedExtend.getMinY()) { |
937 |
double divY = newArea.getMinY() - maxExtend.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(), |
941 |
|
newArea.getMinY() - divY), new Coordinate(newArea |
942 |
|
.getMaxX(), newArea.getMaxY() - divY)); |
943 |
|
|
944 |
|
if (newArea.getMaxY() > maxAllowedExtend.getMaxY()) { |
945 |
|
// LOGGER.debug("Now it exeeds the top border.. cut!"); |
946 |
|
// 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() - divY), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea |
949 |
.getMaxX(), newArea.getMaxY() - divY)); |
.getMaxX(), maxAllowedExtend.getMaxY())); |
950 |
|
|
951 |
if (newArea.getMaxY() > maxExtend.getMaxY()) { |
// LOGGER.debug("and fix aspect ratio"); |
|
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())); |
|
952 |
|
|
953 |
LOGGER.debug("and fix aspect ratio"); |
newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false); |
|
|
|
|
newArea = fixAspectRatio(this.getBounds(), newArea); |
|
|
} |
|
954 |
} |
} |
955 |
|
} |
956 |
|
|
957 |
// Exceeds to the right? move and maybe cut |
// Exceeds to the right? move and maybe cut |
958 |
if (newArea.getMaxX() > maxExtend.getMaxX()) { |
if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) { |
959 |
|
|
960 |
// Move left.. |
// Move left.. |
961 |
double divX = newArea.getMaxX() - maxExtend.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() |
newArea = new Envelope(new Coordinate(newArea.getMinX() - divX, |
965 |
- divX, newArea.getMinY()), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea.getMaxX() |
966 |
.getMaxX() |
- divX, newArea.getMaxY())); |
967 |
- divX, newArea.getMaxY())); |
|
968 |
|
if (newArea.getMinX() < maxAllowedExtend.getMinX()) { |
969 |
if (newArea.getMinX() < maxExtend.getMinX()) { |
// LOGGER.debug("Now it exeeds the left border.. cut!"); |
970 |
LOGGER.debug("Now it exeeds the left border.. cut!"); |
// And cut the left if it moved out of the area |
971 |
// And cut the left if it moved out of the area |
newArea = new Envelope(new Coordinate(maxAllowedExtend.getMinX(), |
972 |
newArea = new Envelope(new Coordinate(maxExtend |
newArea.getMinY()), new Coordinate(newArea |
973 |
.getMinX(), newArea.getMinY()), new Coordinate( |
.getMaxX(), newArea.getMaxY())); |
|
newArea.getMaxX(), newArea.getMaxY())); |
|
974 |
|
|
975 |
LOGGER.debug("and fix aspect ratio"); |
// LOGGER.debug("and fix aspect ratio"); |
976 |
|
|
977 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
978 |
} |
} |
979 |
|
} |
980 |
|
|
981 |
// Exceeds to the left? move and maybe cut |
// Exceeds to the left? move and maybe cut |
982 |
if (newArea.getMinX() < maxExtend.getMinX()) { |
if (newArea.getMinX() < maxAllowedExtend.getMinX()) { |
983 |
|
|
984 |
// Move right.. |
// Move right.. |
985 |
double divX = newArea.getMinX() - maxExtend.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() |
newArea = new Envelope(new Coordinate(newArea.getMinX() - divX, |
989 |
- divX, newArea.getMinY()), new Coordinate(newArea |
newArea.getMinY()), new Coordinate(newArea.getMaxX() |
990 |
.getMaxX() |
- divX, newArea.getMaxY())); |
991 |
- divX, newArea.getMaxY())); |
|
992 |
|
if (newArea.getMaxX() > maxAllowedExtend.getMaxX()) { |
993 |
if (newArea.getMaxX() > maxExtend.getMaxX()) { |
// LOGGER.debug("Now it exeeds the right border.. cut!"); |
994 |
LOGGER.debug("Now it exeeds the right border.. cut!"); |
// And cut the left if it moved out of the area |
995 |
// And cut the left if it moved out of the area |
newArea = new Envelope(new Coordinate(newArea.getMinX(), |
996 |
newArea = new Envelope(new Coordinate( |
newArea.getMinY()), new Coordinate(maxAllowedExtend |
997 |
newArea.getMinX(), newArea.getMinY()), |
.getMaxX(), newArea.getMaxY())); |
|
new Coordinate(maxExtend.getMaxX(), newArea |
|
|
.getMaxY())); |
|
998 |
|
|
999 |
LOGGER.debug("and fix aspect ratio"); |
// LOGGER.debug("and fix aspect ratio"); |
1000 |
|
|
1001 |
newArea = fixAspectRatio(this.getBounds(), newArea); |
newArea = JTSUtil.fixAspectRatio(this.getBounds(), newArea, false); |
|
} |
|
1002 |
} |
} |
|
|
|
1003 |
} |
} |
1004 |
|
|
1005 |
} |
} |
1064 |
|
|
1065 |
/** |
/** |
1066 |
* Returns the evelope of the viewable area. The JMapPane will never show |
* Returns the evelope of the viewable area. The JMapPane will never show |
1067 |
* anything outside of this extend. |
* anything outside of this extend. If this has been set to |
1068 |
|
* <code>null</code> via {@link #setMaxExtend(Envelope)}, it tries to return |
1069 |
|
* quickly the context's bounds. It it takes to long to determine the |
1070 |
|
* context bounds, <code>null</code> is returned. |
1071 |
* |
* |
1072 |
* @param maxExtend |
* @param maxExtend |
1073 |
* <code>null</code> to not have this restriction. |
* <code>null</code> to not have this restriction. |
1074 |
*/ |
*/ |
1075 |
|
|
1076 |
public Envelope getMaxExtend() { |
public Envelope getMaxExtend() { |
1077 |
|
if (maxExtend == null) { |
1078 |
|
try { |
1079 |
|
return fixAspectRatio( |
1080 |
|
this.getBounds(), |
1081 |
|
// Kartenbereich um 10% vergroessern |
1082 |
|
JTSUtil.expandEnvelope(context.getLayerBounds(), 0.1), |
1083 |
|
true |
1084 |
|
); |
1085 |
|
} catch (IOException e) { |
1086 |
|
LOGGER.warn( |
1087 |
|
"maxExtend == null; failed to getLayerBounds of context", |
1088 |
|
e); |
1089 |
|
} |
1090 |
|
} |
1091 |
return maxExtend; |
return maxExtend; |
1092 |
} |
} |
1093 |
|
|