define(["dojo/_base/declare", "esri/layers/ArcGISTiledMapServiceLayer", "esri/tasks/GeometryService", "esri/geometry/Point"], function (declare, ArcGISTiledMapServiceLayer, GeometryService, Point) {
|
var clazz;
|
clazz = declare("Utils", null, {
|
constructor: function () {},
|
createBaseMapLayer: function (layerId, layerURL, isVisible) {
|
var layer = map.getLayer(layerId);
|
if (!layer) {
|
layer = new ArcGISTiledMapServiceLayer(layerURL, {
|
id: layerId,
|
visible: isVisible,
|
layerType: "basemap"
|
})
|
}
|
return layer
|
},
|
createGeometryService: function (layerURL) {
|
var service = new GeometryService(layerURL);
|
return service
|
},
|
lonlat2mercator: function (lonlat) {
|
var mercator = {
|
x: 0,
|
y: 0
|
};
|
var x = lonlat.x * 20037508.34 / 180;
|
var y = Math.log(Math.tan((90 + lonlat.y) * Math.PI / 360)) / (Math.PI / 180);
|
y = y * 20037508.34 / 180;
|
mercator.x = x;
|
mercator.y = y;
|
return mercator
|
},
|
mercator2lonlat: function (mercator) {
|
var lonlat = {
|
x: 0,
|
y: 0
|
};
|
var x = mercator.x / 20037508.34 * 180;
|
var y = mercator.y / 20037508.34 * 180;
|
y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);
|
lonlat.x = x;
|
lonlat.y = y;
|
return lonlat
|
},
|
getDistance: function (x1, y1, x2, y2) {
|
var point1 = new Point(x1, y1, map.spatialReference);
|
var point2 = new Point(x2, y2, map.spatialReference);
|
var dis = esri.geometry.getLength(point1, point2);
|
return dis
|
},
|
getStringGeo: function (points) {
|
var pointReg;
|
pointReg = /(\d+.?\d*)\s+(\d+.?\d*)/g;
|
var lineReg = /\((\s?\d+\.\d*\s+\d+\.\d*\,?)+\)/g;
|
var graphicReg;
|
graphicReg = /(\((((\(((\d+.?\d*)\s+(\d+.?\d*),?)+\)),?)+),?\)),?/g;
|
var wkid = 4326;
|
if (points.indexOf("POINT") == 0) {
|
var pr = [],
|
pt = [],
|
prarr = [];
|
if (points.indexOf("-") > -1) {
|
points = points.replace("POINT (", "").replace(")", "");
|
var x1 = points.split(" ")[0];
|
var y1 = points.split(" ")[1];
|
pr.push(parseFloat(x1));
|
pr.push(parseFloat(y1));
|
} else {
|
var pointResult = pointReg.exec(points);
|
pr.push(parseFloat(pointResult[1]));
|
pr.push(parseFloat(pointResult[2]));
|
}
|
pt.push(pr);
|
prarr.push(pt);
|
return prarr;
|
} else if (points.indexOf("LINESTRING") == 0 || points.indexOf("MULTILINESTRING") == 0) {
|
var paths = new Array();
|
var lineResult = lineReg.exec(points);
|
while (lineResult != null) {
|
var path = new Array();
|
var pathResult = pointReg.exec(lineResult[0]);
|
while (pathResult) {
|
var pathPoint = new Array();
|
pathPoint.push(Number(pathResult[1]));
|
pathPoint.push(Number(pathResult[2]));
|
path.push(pathPoint);
|
pathResult = pointReg.exec(lineResult[0])
|
}
|
paths.push(path);
|
lineResult = lineReg.exec(points)
|
}
|
return paths;
|
} else if (points.indexOf("POLYGON") == 0 || points.indexOf("MULTIPOLYGON") == 0) {
|
var rings;
|
rings = new Array();
|
var polygonResult = lineReg.exec(points);
|
while (polygonResult != null) {
|
var ring;
|
ring = new Array();
|
var ringResult;
|
ringResult = pointReg.exec(polygonResult[0]);
|
while (ringResult) {
|
var ringPoint = new Array();
|
ringPoint.push(Number(ringResult[1]));
|
ringPoint.push(Number(ringResult[2]));
|
ring.push(ringPoint);
|
ringResult = pointReg.exec(polygonResult[0])
|
}
|
rings.push(ring);
|
polygonResult = lineReg.exec(points)
|
}
|
return rings;
|
}
|
return null
|
}
|
});
|
clazz.getInstance = function () {
|
if (instance === null) {
|
instance = new clazz()
|
}
|
return instance
|
};
|
return clazz
|
});
|