-
Notifications
You must be signed in to change notification settings - Fork 3
/
Projection.js
254 lines (221 loc) · 9.22 KB
/
Projection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
define([
'dojo/_base/declare',
'dijit/_WidgetBase',
'dijit/_TemplatedMixin',
'dijit/_WidgetsInTemplateMixin',
'dojox/grid/DataGrid',
'dojo/data/ItemFileWriteStore',
'dojo/_base/lang',
'dojo/topic',
'dojo/aspect',
'esri/layers/GraphicsLayer',
'esri/graphic',
'esri/renderers/SimpleRenderer',
'esri/symbols/PictureMarkerSymbol',
'esri/graphicsUtils',
'esri/geometry/Point',
'esri/SpatialReference',
'esri/geometry/Extent',
'proj4js/proj4',
'dojo/text!./Projection/templates/Projection.html',
'xstyle/css!./Projection/css/Projection.css'
], function (
declare,
_WidgetBase,
_TemplatedMixin,
_WidgetsInTemplateMixin,
DataGrid, ItemFileWriteStore,
lang, topic, aspect,
GraphicsLayer, Graphic, SimpleRenderer, PictureMarkerSymbol, graphicsUtils, Point, SpatialReference, Extent,
proj4,
template
) {
return declare([_WidgetBase, _TemplatedMixin, _WidgetsInTemplateMixin], {
map: null,
widgetsInTemplate: true,
templateString: template,
baseClass: 'gis_ProjectionDijit',
// proj4BaseURL default: http://spatialreference.org/ (no ssl) or //epsg.io/, or /local/folder/
proj4BaseURL: '//epsg.io/',
// options are ESRI, EPSG and SR-ORG
// See http://spatialreference.org/ for more information
baseProjection: null,
proj4Catalog: 'EPSG', // case sensitive!
postCreate: function () {
this.inherited(arguments);
var map = this.map;
if (!map) {
console.log('MapInfo error::a map reference is required');
this.destroy();
return;
}
if (!window.proj4) {
window.proj4 = proj4;
}
this.pointSymbol = new PictureMarkerSymbol(require.toUrl('gis/dijit/Projection/images/crosshair32.png'), 32, 32);
this.pointGraphics = new GraphicsLayer({
id: 'test_graphics',
title: 'Test'
});
this.pointRenderer = new SimpleRenderer(this.pointSymbol);
this.pointRenderer.label = 'test';
this.pointRenderer.description = 'test';
this.pointGraphics.setRenderer(this.pointRenderer);
this.map.addLayer(this.pointGraphics);
this._createProjectionGrid();
this.own(topic.subscribe('mapClickMode/currentSet', lang.hitch(this, 'setMapClickMode')));
if (this.parentWidget && this.parentWidget.toggleable) {
this.own(aspect.after(this.parentWidget, 'toggle', lang.hitch(this, function () {
this.clearProjections();
})));
}
//initialize when map loaded
if (map.loaded) {
this._initialize(map);
} else {
map.on('load', lang.hitch(this, '_initialize', map));
}
},
setMapClickMode: function (mode) {
this.mapClickMode = mode;
},
_createProjectionGrid: function () {
var data = {
identifier: 'id',
items: []
};
for (var i = 0; i < this.projectionList.length; i++) {
data.items.push({ 'id': i, 'name': this.projectionList[i].title, 'x': '', 'y': '' });
}
this.projectionStore = new ItemFileWriteStore({ data: data });
var layout = [
{ 'name': 'Name', 'field': 'name', 'width': '24%', 'noresize': 'true', styles: 'bold;text-align: left;' },
{ 'name': 'x', 'field': 'x', 'width': '38%', 'noresize': 'true', 'editable': 'true', styles: 'text-align: left;' },
{ 'name': 'y', 'field': 'y', 'width': '38%', 'noresize': 'true', 'editable': 'true', styles: 'text-align: left;' }
];
this.projectionGrid = new DataGrid({
cellNavigation: false,
showHeader: true,
store: this.projectionStore,
structure: layout,
canSort: false,
singleClickEdit: true,
autoHeight: true
}, this.projectionGrid);
this.projectionGrid.on('ApplyEdit', lang.hitch(this, '_reProjection'));
this.projectionGrid.startup();
},
_initialize: function (map) {
map.on('click', lang.hitch(this, function (evt) {
if (this.mapClickMode === 'projection') {
this._onClick(evt);
}
}));
var wkid = this.map.spatialReference.wkid;
if (wkid === 102100) { // ESRI --> EPSG
wkid = 3857;
}
this.baseProjection = this.proj4Catalog + ':' + String(wkid);
if (this.proj4BaseURL.slice(-1) != '/')
this.proj4BaseURL += '/';
//load projection for each srid
for (var i = 0; i < this.projectionList.length; i++) {
var url = this.proj4BaseURL + String(this.projectionList[i].srid) + '.js';
require([url]);
}
this.clearProjections();
},
_onClick: function (evt) {
this._project(evt.mapPoint);
},
_project: function (pnt) {
this._drawMarker(pnt);
for (var i = 0; i < this.projectionList.length; i++) {
var key = this.proj4Catalog + ':' + String(this.projectionList[i].srid);
var projPnt = proj4(proj4.defs[this.baseProjection], proj4.defs(key)).forward([pnt.x, pnt.y]);
var item = this.projectionGrid.getItem(i);
if (proj4.defs(key)) {
var precision = proj4.defs(key).units == 'm' ? 2 : 7; // hack: if it ain't metres, it's degrees
this.projectionGrid.store.setValue(item, 'x', projPnt[0].toFixed(precision));
this.projectionGrid.store.setValue(item, 'y', projPnt[1].toFixed(precision));
} else {
this.projectionGrid.store.setValue(item, 'x', 'Error');
this.projectionGrid.store.setValue(item, 'y', 'Error');
//item.customStyles += 'background-color:#FFB93F;'
}
}
},
_reProjection: function (rowIndex) {
var item = this.projectionGrid.getItem(rowIndex);
// project back to base map coords
var key = this.proj4Catalog + ':' + String(this.projectionList[rowIndex].srid);
if (proj4.defs(key)) {
var pnt = proj4(proj4.defs(key), proj4.defs[this.baseProjection]).forward([item.x, item.y]);
// move marker and project from new position
var point = new Point(pnt[0], pnt[1], new SpatialReference({ wkid: this.map.spatialReference.wkid }));
this._project(point);
this.zoomtoMarker(); // maybe turn this off or option
}
},
_drawMarker: function (pnt) {
var graphic;
this.pointGraphics.clear();
graphic = new Graphic(pnt);
this.pointGraphics.add(graphic);
},
toggleMode: function (val) {
if (val) {
this.locatePoint();
} else {
this.cancelProjection();
}
},
locatePoint: function () {
this.map.setMapCursor('crosshair');
topic.publish('mapClickMode/setCurrent', 'projection');
},
cancelProjection: function () {
this.map.setMapCursor('auto');
topic.publish('mapClickMode/setDefault');
},
clearProjections: function () {
dijit.byId('toggleme').set('checked', false); // will raise the toggleMode event
for (var i = 0; i < this.projectionList.length; i++) {
var item = this.projectionGrid.getItem(i);
this.projectionGrid.store.setValue(item, 'x', '');
this.projectionGrid.store.setValue(item, 'y', '');
}
this.pointGraphics.clear();
},
zoomtoMarker: function () {
var zoomExtent = null;
if (this.pointGraphics.graphics.length > 0) {
zoomExtent = this.getPointFeaturesExtent(this.pointGraphics.graphics);
}
this.map.setExtent(zoomExtent.expand(1.2));
// if there is a poinmt in the layer, then go to it.
// or read the values entered in current tab, reproject?, and zoom to
},
getPointFeaturesExtent: function (pointFeatures) {
var extent = graphicsUtils.graphicsExtent(pointFeatures);
if (extent === null && pointFeatures.length > 0) {
extent = this.getExtentFromPoint(pointFeatures[0]);
}
return extent;
},
getExtentFromPoint: function (point) {
var sz = this.pointExtentSize; // hack
var pt = point.geometry;
var extent = new Extent({
'xmin': pt.x - sz,
'ymin': pt.y - sz,
'xmax': pt.x + sz,
'ymax': pt.y + sz,
'spatialReference': {
wkid: this.map.spatialReference.wkid
}
});
return extent;
}
});
});