📄 webextent.java
字号:
package com.esri.solutions.jitk.web.data.geometry;
import com.esri.adf.web.data.geometry.WebPoint;
import com.esri.adf.web.data.geometry.WebPolygon;
import com.esri.adf.web.data.geometry.WebRing;
/**
* Geometry extent type that extends the ADF {@link com.esri.adf.web.data.geometry.WebExtent}
* type and adds capabilities, such as conversion to a {@link WebPolygon}
*/
public class WebExtent extends com.esri.adf.web.data.geometry.WebExtent {
private static final long serialVersionUID = -8012409567364871347L;
/**
* Constructor to create a {@link WebExtent} from a
* {@link com.esri.adf.web.data.geometry.WebExtent}
* @param adfExtent
*/
public WebExtent(com.esri.adf.web.data.geometry.WebExtent adfExtent) {
this.setSpatialReference(adfExtent.getSpatialReference());
this.setMaxX(adfExtent.getMaxX());
this.setMaxY(adfExtent.getMaxY());
this.setMinX(adfExtent.getMinX());
this.setMinY(adfExtent.getMinY());
}
/**
* Converts the {@link com.esri.adf.web.data.geometry.WebExtent} type
* to a {@link WebPolygon}
* @return The {@link WebExtent} converted to a {@link WebPolygon}
*/
public WebPolygon toWebPolygon() {
double minx = this.getMinX();
double miny = this.getMinY();
double maxx = this.getMaxX();
double maxy = this.getMaxY();
WebPoint ll = new WebPoint(minx, miny);
WebPoint ul = new WebPoint(minx, maxy);
WebPoint ur = new WebPoint(maxx, maxy);
WebPoint lr = new WebPoint(maxx, miny);
WebRing wr = new WebRing();
wr.addPoint(ll);
wr.addPoint(ul);
wr.addPoint(ur);
wr.addPoint(lr);
wr.addPoint(ll);
WebPolygon wp = new WebPolygon();
wp.addRing(wr);
return wp;
}
/**
* Calculates and returns the center point of the extent.
* WARNING: This calculation does not work for extents crossing the
* North or South pole and 180 degrees longitude.
* @return {@link WebPoint} representing the center point
*/
public WebPoint getCenterPoint() {
WebPoint center = new WebPoint();
double x1 = getMinX();
double x2 = getMaxX();
double y1 = getMinY();
double y2 = getMaxY();
center.setX((x1 + x2) / 2);
center.setY((y1 + y2) / 2);
center.setSpatialReference(this.getSpatialReference());
return center;
}
/**
* Adjust the extent to the specified aspect ration for width and heigh
* so that the extent is adjust
* @param width
* @param height
* @return
*/
public WebExtent getAdjustedExtent(int width, int height) {
com.esri.solutions.jitk.web.data.geometry.WebExtent adjustedExt =
new com.esri.solutions.jitk.web.data.geometry.WebExtent(this);
double extWidth = getWidth();
double extHeigth = getHeight();
if ((extWidth <= 0) || (extWidth <= 0) || (extWidth <= 0) || (extWidth <= 0)) {
new Exception("Extent dimesion and passed dimesions can not equal to or less than zero");
}
double mapAspectRation = (double) width / (double) height;
double extAspectRation = extWidth / extHeigth;
double shrinkFactor = 1.0;
WebPoint center = adjustedExt.getCenterPoint();
/*
* aspect ration = width / height
*
* If map aspect ratio > extent aspect ratio
* shrink extent height
* Else
* shrink extent width
*/
if (mapAspectRation > extAspectRation) {
shrinkFactor = extAspectRation * (1 / mapAspectRation);
double yzoom = ( (extHeigth * shrinkFactor) / (double) 2.0);
adjustedExt.setMaxY(center.getY() + yzoom);
adjustedExt.setMinY(center.getY() - yzoom);
} else {
shrinkFactor = mapAspectRation * (1 / extAspectRation);
double xzoom = ( (extWidth * shrinkFactor) / (double) 2.0);
adjustedExt.setMaxX(center.getX() + xzoom);
adjustedExt.setMinX(center.getX() - xzoom);
}
return adjustedExt;
}
/**
* Centers the extent at the specified {@link WebPoint}
* @param mapPoint Point to center on
* @param zoomFactor Zoom factor of the centering operation
*/
public void centerAt(WebPoint mapPoint, double zoomFactor) {
double x1 = getMinX();
double x2 = getMaxX();
double y1 = getMinY();
double y2 = getMaxY();
double cx = (x1 + x2) / 2;
double cy = (y1 + y2) / 2;
double dx = mapPoint.getX() - cx;
double dy = mapPoint.getY() - cy;
double xzoom = (1 - zoomFactor) * getWidth() / 2;
double yzoom = (1 - zoomFactor) * getHeight() / 2;
this.putCoords(x1 + dx + (xzoom * x1), y1 + dy + (yzoom * y1), x2 + dx - (xzoom * x2), y2 + dy - (yzoom * y2));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -