123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- /**
- * @fileoverview 工具 测量地图面积 模块
- * @author Song.Huang
- * @version 1.0.0
- */
- define(['vendorDir/map23dlib/leaflet.mapshot',
- 'css!vendorDir/map23dlib/leaflet.mapshot'],
- function(){
- /**
- * 状态值
- * @type {Boolean}
- * @default false
- * @private
- */
- var status ={
- initialized:false//是否初始化
- }
- /**
- * 模块数据 用于数据存储和外部调用
- * @type {Object}
- * 数据存放
- */
- var modValue ={
- loopAjaxTimeOut:{},
- time_stamp:'',
- postData:null,
- mapShotControl:null,
- BLC: [200000000, 100000000, 50000000, 25000000, 12000000, 6000000, 1600000, 1550000, 800000, 400000, 200000, 100000, 50000, 25000, 12000, 2300, 3000, 1500, 800]
- }
- var status = {
- countingLock:false
- }
- /**
- * 初始化
- * 监听事件
- * @type {Function}
- */
- function init(){
- if(!status.initialized){
- status.initialized = true;
- setLayout();
- bindEvent();
- subscribe();
- modValue.mapShotControl = L.control.mapShot().addTo(map2DViewer.map);
- //map2DViewer.map.on('mapShot:postData',modValue.mapShotPostData,this);
- }
- ONEMAP.C.publisher.publish({
- modName:'toolMapShot'
- },'tools:active');
- }
- function setLayout(){
- $('body').append($('<div id="mapShotDownload"><p></p></div>'));
- $(window).resize(function() {
- layoutResize();
- });
- };
- function bindEvent(){
-
- };
- function layoutResize(){
-
- };
- function mapfishPostData(postData){
- var newPostData = encodeURIComponent(changeMapShotData(postData));
- // console.log(modValue.mapShotDrawData)
- //var appId = 'print_osm_new_york_EPSG_900913';
- var appId = 'print_osm_EPSG3857';
- var format = postData.ext;
- //console.log(changeMapShotData(postData));
- var startTime = new Date().getTime();
- console.log(newPostData);
- ONEMAP.C.publisher.publish({ type: 'success', message: '开始下载' }, 'noteBar::add');
- //return false;
- $.ajax({
- type: 'POST',
- url: onemapUrlConfig.mapFishServerUrl+'/print/' + appId + '/report.' + format,
- data: newPostData,
- success: function(data) {
- downloadWhenReady(startTime, data);
- },
- error: function(data) {
- ONEMAP.C.publisher.publish({ type: 'error', message: '下载出错' }, 'noteBar::add');
- },
- dataType: 'json'
- });
- };
- function downloadWhenReady(startTime, data) {
- if ((new Date().getTime() - startTime) > 30000) {
- ONEMAP.C.publisher.publish({ type: 'success', message: '准备下载' }, 'noteBar::add');
- } else {
- updateWaitingMsg(startTime, data);
- setTimeout(function() {
- $.getJSON(onemapUrlConfig.mapFishServerUrl + data.statusURL, function(statusData) {
- if (!statusData.done) {
- downloadWhenReady(startTime, data);
- } else {
- window.location = onemapUrlConfig.mapFishServerUrl + statusData.downloadURL;
- //ONEMAP.C.publisher.publish({ type: 'success', message: 'Downloading: ' + data.ref }, 'noteBar::add');
- }
- }, function error(data) { ONEMAP.C.publisher.publish({ type: 'error', message: '下载出错' }, 'noteBar::add') });
- }, 1000);
- }
- }
- function changeMapShotData(postData) {
- //计算截图中心点
- var scale = modValue.BLC[postData.zoom - 1];
- var mapcenter = lonlatToMercator(postData.center);
- var geojsonFeatures = [];
- var geojsonStyles = {};
- // modValue.mapShotDrawData = {}
- // modValue.mapShotDrawData = map2DViewer.map._layers;
- //获取截图元素数据
- // $.each(modValue.mapShotDrawData,function(i,t){
- $.each(map2DViewer.map._layers,function(i,t){
- var guid = map23DControl.buildGuid('mapfish_style');
- // if(t.drawType == 'marker'){
- if(t.shape == 'Marker'){
- // var curmarkerCoor = stringToParse([t.drawInfo.center[1],t.drawInfo.center[0]]);
- var curmarkerCoor = stringToParse([t._latlng.lng,t._latlng.lat]);
- curmarkerCoor = lonlatToMercator(curmarkerCoor)
- var fItem = {
- "geometry": {
- "coordinates": curmarkerCoor,
- "type": "Point"
- },
- "properties": {
- "_gx_style": guid
- },
- "type": "Feature"
- };
- var iconUrl = onemapUrlConfig.siteUrl+'/scripts/vendor/leaflet/draw_new/images/marker-icon.png';
- // if(t.options.icon){
- // iconUrl = t.options.icon.options.iconUrl;
- // }
- geojsonStyles[guid] = {
- "fillColor": "#ff0000",
- "fillOpacity": 1,
- "pointRadius": 5,
- "externalGraphic":iconUrl,
- "graphicOpacity":1,
- "graphicFormat":"image/png"
- };
- geojsonFeatures.push(fItem);
- // }else if(t.drawType == 'polyline'){
- }else if(t.shape == 'polyline'){
- var polylinescoor = [];
- for (var i = 0; i < t.drawInfo.LatLngs.length; i++) {
- curpolylinescoor = stringToParse([t.drawInfo.LatLngs[i].lng,t.drawInfo.LatLngs[i].lat]);
- curpolylinescoor = lonlatToMercator(curpolylinescoor);
- polylinescoor.push(curpolylinescoor);
- }
- var guid = guid;
- var fItem = {
- "geometry": {
- "coordinates": polylinescoor,
- "type": "LineString"
- },
- "properties": {
- "_gx_style": guid
- },
- "type": "Feature"
- };
- geojsonStyles[guid] = {
- "strokeColor": t.drawInfo.shapeOptions.color,
- "strokeDashstyle": "solid",
- "strokeLinecap": "round",
- "strokeOpacity": t.drawInfo.shapeOptions.opacity,
- "strokeWidth": t.drawInfo.shapeOptions.weight
- };
- geojsonFeatures.push(fItem);
- // }else if(t.drawType == 'polygon'){
- }else if(t.shape == 'polygon'){
- var polygonscoor = [];
- polygonscoor[0] = [];
- for (var i = 0; i < t.drawInfo.LatLngs[0].length+1; i++) {
- if(i<t.drawInfo.LatLngs[0].length){
- var curpolygonscoor = stringToParse([t.drawInfo.LatLngs[0][i].lng,t.drawInfo.LatLngs[0][i].lat]);
- curpolygonscoor = lonlatToMercator(curpolygonscoor);
- polygonscoor[0].push(curpolygonscoor);
- }else{
- var curpolygonscoor = stringToParse([t.drawInfo.LatLngs[0][0].lng,t.drawInfo.LatLngs[0][0].lat]);
- curpolygonscoor = lonlatToMercator(curpolygonscoor);
- polygonscoor[0].push(curpolygonscoor);
- }
-
- }
- var fItem = {
- "geometry": {
- "coordinates": polygonscoor,
- "type": "Polygon"
- },
- "properties": {
- "_gx_style": guid
- },
- "type": "Feature"
- };
- geojsonStyles[guid] = {
- "fillColor": t.drawInfo.shapeOptions.fillColor,
- "fillOpacity": t.drawInfo.shapeOptions.fillOpacity,
- "strokeColor": t.drawInfo.shapeOptions.color,
- "strokeDashstyle": "solid",
- "strokeLinecap": "round",
- "strokeOpacity": t.drawInfo.shapeOptions.opacity,
- "strokeWidth": t.drawInfo.shapeOptions.weight
- };
- geojsonFeatures.push(fItem);
- }else if(t.drawType == 'circle'){
- //圆心
- var allCirclePoint = [];
- var center_lat = t.drawInfo.startLatLng[0];
- var center_lon = t.drawInfo.startLatLng[1];
- //半径
- var radius = t.drawInfo.radius; //米
- //转换定义 核心
- var epsg_4326 = "+proj=longlat +datum=WGS84 +no_defs "
- var esri_102016 = "+proj=aeqd +lat_0=90 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs"
- var self_defined = "+proj=aeqd +lat_0=" + center_lat + "+lon_0=" + center_lon + "+x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs"
- //生成圆平面点;
- var pointArr = L.Util.getCirclePoint(0, 0, radius);
- //圆的轮廓点转换为经纬度,当做GeoJSON来使用。
- var circlePoints = [];
- for (var k = 0; k < pointArr.length; k++) {
- var item = pointArr[k];
- //核心 将平面坐标转经纬度坐标
- var xy = proj4(self_defined, epsg_4326, [item.x, item.y]);
- circlePoints.push([
- xy[0],
- xy[1]
- ])
- }
- var polygonscoor = [];
- polygonscoor[0] = [];
- for (var i = 0; i < circlePoints.length+1; i++) {
- if(i<circlePoints.length){
- var curpolygonscoor = stringToParse([circlePoints[i][0],circlePoints[i][1]]);
- curpolygonscoor = lonlatToMercator(curpolygonscoor);
- polygonscoor[0].push(curpolygonscoor);
- }else{
- var curpolygonscoor = stringToParse([circlePoints[0][0],circlePoints[0][1]]);
- curpolygonscoor = lonlatToMercator(curpolygonscoor);
- polygonscoor[0].push(curpolygonscoor);
- }
-
- }
- var fItem = {
- "geometry": {
- "coordinates": polygonscoor,
- "type": "Polygon"
- },
- "properties": {
- "_gx_style": guid
- },
- "type": "Feature"
- };
- geojsonStyles[guid] = {
- "fillColor": t.drawInfo.shapeOptions.fillColor,
- "fillOpacity": t.drawInfo.shapeOptions.fillOpacity,
- "strokeColor": t.drawInfo.shapeOptions.color,
- "strokeDashstyle": "solid",
- "strokeLinecap": "round",
- "strokeOpacity": t.drawInfo.shapeOptions.opacity,
- "strokeWidth": t.drawInfo.shapeOptions.weight
- };
- geojsonFeatures.push(fItem);
- // }else if(t.drawType == 'rectangle'){
- }else if(t.shape == 'rectangle'){
- var polygonscoor = [];
- polygonscoor[0] = [];
- var LatLngs = [];
- // LatLngs.push([t.drawInfo.bounds._southWest.lng,t.drawInfo.bounds._northEast.lat]);
- // LatLngs.push([t.drawInfo.bounds._southWest.lng,t.drawInfo.bounds._southWest.lat]);
- // LatLngs.push([t.drawInfo.bounds._northEast.lng,t.drawInfo.bounds._southWest.lat]);
- // LatLngs.push([t.drawInfo.bounds._northEast.lng,t.drawInfo.bounds._northEast.lat]);
- // LatLngs.push([t.drawInfo.bounds._southWest.lng,t.drawInfo.bounds._northEast.lat]);
- // for (var i = 0; i < LatLngs.length; i++) {
- // var curpolygonscoor = stringToParse([LatLngs[i][0],LatLngs[i][1]]);
- // curpolygonscoor = lonlatToMercator(curpolygonscoor);
- // polygonscoor[0].push(curpolygonscoor);
- // }
- var fItem = {
- "geometry": {
- // "coordinates": polygonscoor,
- "coordinates": t._latlngs,
- "type": "Polygon"
- },
- "properties": {
- "_gx_style": guid
- },
- "type": "Feature"
- };
- geojsonStyles[guid] = {
- "fillColor": t.drawInfo.shapeOptions.fillColor,
- "fillOpacity": t.drawInfo.shapeOptions.fillOpacity,
- "strokeColor": t.drawInfo.shapeOptions.color,
- "strokeDashstyle": "solid",
- "strokeLinecap": "round",
- "strokeOpacity": t.drawInfo.shapeOptions.opacity,
- "strokeWidth": t.drawInfo.shapeOptions.weight
- };
- geojsonFeatures.push(fItem);
- }
- })
- geojsonStyles.styleProperty = "_gx_style";
- geojsonStyles.version = 1;
- var curpostData = {
- "attributes": {
- "description": postData.mapshotcopyright,
- "map": {
- "center": mapcenter,
- "dpi": 72,
- "layers": [{
- "geoJson": {
- "features": geojsonFeatures,
- "type": "FeatureCollection"
- },
- "style": geojsonStyles,
- "type": "geojson"
- }, {
- // "baseURL": map23DConfig.tileServerUrl + "/" + postData.guid + "?l={z}&x={x}&y={y}",
- "baseURL": map23DConfig.tileServerFo3DUrl + "/" + postData.guid + "?l={z}&x={x}&y={y}",
- "imageExtension": "png",
- "type": "OSM"
- }],
- "projection": "EPSG:3857",
- "rotation": 0,
- "scale": scale
- },
- "title": postData.mapshotName
- },
- "layout": "A4 landscape"
- }
- return postData = JSON.stringify(curpostData);
- };
- function updateWaitingMsg(startTime, data) {
- var elapsed = Math.floor((new Date().getTime() - startTime) / 100);
- var time = '';
- if (elapsed > 5) {
- time = (elapsed / 10) + " sec";
- }
- ONEMAP.C.publisher.publish({ type: 'success', message: '等候时间' + time }, 'noteBar::add');
- }
- //经纬度转魔卡托
- function lonlatToMercator(lonlat) {
- var mercator = [];
- var x = lonlat[0] * 20037508.34 / 180;
- var y = Math.log(Math.tan((90 + lonlat[1]) * Math.PI / 360)) / (Math.PI / 180);
- y = y * 20037508.34 / 180;
- mercator.push(x);
- mercator.push(y);
- return mercator;
- }
- function stringToParse(lnglat) {
- var x = JSON.parse(lnglat[0]);
- var y = JSON.parse(lnglat[1]);
- var parsecoor = [x, y];
- return parsecoor;
- }
- function mapShotPostData(postData){
- modValue.postData = postData;
- var LTpx = map2DViewer.map.project(
- new L.LatLng(JSON.parse(postData.bbox)[1],JSON.parse(postData.bbox)[0]),
- postData.zoom
- );
- var RBpx = map2DViewer.map.project(
- new L.LatLng(JSON.parse(postData.bbox)[3],JSON.parse(postData.bbox)[2]),
- postData.zoom
- );
- var countDownTF = parseInt(((RBpx.x-LTpx.x)*(RBpx.y-LTpx.y))/(5120*5120));
- if(countDownTF > onemapUrlConfig.mapShotMaxPicCount){
- var xiangsu = parseInt(5120*Math.sqrt(onemapUrlConfig.mapShotMaxPicCount));
- /*$('#mapShotDownload p').html('当前范围产生'+countDownTF+'个图幅数,'+
- '<br/>已经超过单次下载'+onemapUrlConfig.mapShotMaxPicCount+'个最大图幅数(5120×5120像素/图幅),请分开截图!<br/>'+
- '<span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">修改区域</span>')*/
- $('#mapShotDownload p').html('地图拼接下载像素超过了'+xiangsu+'×'+xiangsu+'像素,请缩小截图下载区域<br/>'+
- '<span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">修改区域</span>')
- $('#mapShotDownload').show();
- return false;
- }
- // if((RBpx.x-LTpx.x)>onemapUrlConfig.mapShotMaxPicWidth){
- // alert('截图宽度不能大于 '+onemapUrlConfig.mapShotMaxPicWidth+' 像素!');
- // return false;
- // }
- // if((RBpx.y-LTpx.y)>onemapUrlConfig.maxPicHeight){
- // alert('截图高度不能大于 '+onemapUrlConfig.maxPicHeight+' 像素!');
- // return false;
- // }
-
- $('#mapShotDownload').show();
- var nDate = new Date();
- modValue.time_stamp = nDate.getTime();
- var url = onemapUrlConfig.mapShotDownServerUrl+'/puzzles/?bbox='+postData.bbox+'&zoom='+postData.zoom+'&type='+postData.guid+'&ext='+postData.ext+'&time_stamp='+modValue.time_stamp+'&name='+encodeURI(postData.name);
- $.ajax({
- type:"get",
- url:url,
- dataType:'jsonp',
- jsonp:'callback',
- success:function(data){
- //modValue._loopGetResult({ajaxKey:'1'});
- }
- });
- setTimeout(function(){
- loopGetResult({ajaxKey:modValue.time_stamp});
- }, 1000);
- };
- function againMapShotPostData(){
- mapShotPostData(modValue.postData);
- }
- function loopGetResult(options){
- $.ajax({
- url:onemapUrlConfig.mapShotDownServerUrl+'/processing/'+modValue.time_stamp,
- type:"get",
- dataType:'jsonp',
- jsonp:'callback',
- success:function(data){
- if(data.processing == ''){
- $('#mapShotDownload p').html('服务器处理失败,请重试! <br/><span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.againMapShot()">再次提交</span>');
- }
- if(data.processing.length<3){
- if(modValue.loopAjaxTimeOut.hasOwnProperty(options.ajaxKey)){
- clearTimeout(modValue.loopAjaxTimeOut[options.ajaxKey]);
- }
- $('#mapShotDownload p').html('服务器正在截图,截图进度 '+data.processing +' %. <br/><span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">取消截图</span>');
- modValue.loopAjaxTimeOut[options.ajaxKey] = setTimeout(function(){
- loopGetResult(options);
- },1000);
- }else if(data.processing == '100'){
- if(modValue.loopAjaxTimeOut.hasOwnProperty(options.ajaxKey)){
- clearTimeout(modValue.loopAjaxTimeOut[options.ajaxKey]);
- }
- $('#mapShotDownload p').html('服务器已完成截图,正在进行打包. <br/><span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">取消截图</span>');
- modValue.loopAjaxTimeOut[options.ajaxKey] = setTimeout(function(){
- loopGetResult(options);
- },1000);
- }else {
- delete modValue.loopAjaxTimeOut[options.ajaxKey];
- //获取最终结果
- $('#mapShotDownload p').html('服务器截图完成,<a target="_blank" href="'+onemapUrlConfig.mapShotDownServerUrl+'/image_zip/'+data.processing+'">点击下载!</a><br/><span id="continueMapShot" onclick="ONEMAP.M.toolMapShot.continueMapShot()">继续截图</span><span style="cursor:pointer" onclick="ONEMAP.M.toolMapShot.exitMapShot();">退出截图</span>')
- //alert(data);
- }
- }
- });
- };
- function continueMapShot(){
- if(modValue.loopAjaxTimeOut.hasOwnProperty(modValue.time_stamp)){
- clearTimeout(modValue.loopAjaxTimeOut[modValue.time_stamp]);
- }
- $('#mapShotDownload').hide().html('<p>正在提交截图数据到服务器</p>');
- }
- function exitMapShot(){
- $('#mapshotScreen').click();
- modValue.mapShotControl.remove();
- $('#mapShotDownload').hide()
- }
- /**
- * 注册监听
- */
- function subscribe(){
- ONEMAP.C.publisher.subscribe(remove, 'tools:active');
- ONEMAP.C.publisher.subscribe(mapShotPostData,'mapShot:postData');
- ONEMAP.C.publisher.subscribe(mapfishPostData,'mapFish:postData');
- };
- /**
- * 功能移除
- */
- function remove(options){
- if(options.modName != 'toolMapShot'){
- $('.tools-mapshot').removeClass('cur')
- modValue.mapShotControl.remove();
- }else{
- if($('.tools-mapshot').hasClass('cur')){
- $('.tools-mapshot').removeClass('cur');
- modValue.mapShotControl.remove();
- }else{
- $('.tools-mapshot').addClass('cur');
- modValue.mapShotControl.init();
- }
- }
- };
- return ONEMAP.M.toolMapShot = {
- init:init,
- continueMapShot:continueMapShot,
- exitMapShot:exitMapShot
- }
- });
|