Skip to content

Commit

Permalink
Merge pull request #724 from KaiVolland/misc
Browse files Browse the repository at this point in the history
Fix `fetchProj4jCrsDefinitions` and linting
  • Loading branch information
KaiVolland authored Mar 31, 2023
2 parents ed577d6 + 9de125a commit 9a9dfba
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
],
"max-len": [
2,
80,
100,
4
],
"one-var": [
Expand Down
45 changes: 23 additions & 22 deletions src/util/Projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,36 @@ Ext.define('BasiGX.util.Projection', {
}
var epsgIoBaseUrl = 'https://epsg.io/?q={0}&format=json';
var projectionsStore = BasiGX.store.Projections;
var epsgPromises = Ext.Array.map(Ext.Array.unique(epsgCodeArray), function (epsgCodeStr) {
var epsgCode = epsgCodeStr.toUpperCase().replace('EPSG:', '');
var existingDef = projectionsStore.getById(epsgCodeStr.toUpperCase());
var epsgPromises = Ext.Array.map(
Ext.Array.unique(epsgCodeArray), function (epsgCodeStr) {
var epsgCode = epsgCodeStr.toUpperCase().replace('EPSG:', '');
var existingDef = projectionsStore.getById(epsgCode);

if (existingDef) {
return Ext.Promise.resolve(existingDef.getData());
}
if (existingDef) {
return Ext.Promise.resolve(existingDef.getData());
}

return new Ext.Promise(function (resolve, reject) {
var epsgUrl = Ext.String.format(epsgIoBaseUrl, epsgCode);
Ext.Ajax.request({
url: epsgUrl,
useDefaultXhrHeader: false,
success: function (response) {
if (response && response.responseText &&
return new Ext.Promise(function (resolve, reject) {
var epsgUrl = Ext.String.format(epsgIoBaseUrl, epsgCode);
Ext.Ajax.request({
url: epsgUrl,
useDefaultXhrHeader: false,
success: function (response) {
if (response && response.responseText &&
response.status === 200) {
var resultObj = Ext.decode(response.responseText);
projectionsStore.add(Ext.apply({}, resultObj.results[0]));
resolve(resultObj.results[0]);
} else {
var resultObj = Ext.decode(response.responseText);
projectionsStore.add(Ext.apply({}, resultObj.results[0]));
resolve(resultObj.results[0]);
} else {
reject(response.status);
}
},
failure: function (response) {
reject(response.status);
}
},
failure: function (response) {
reject(response.status);
}
});
});
});
});
return Ext.Promise.all(epsgPromises);
},

Expand Down
2 changes: 1 addition & 1 deletion src/view/panel/CoordinateMousePositionPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ Ext.define('BasiGX.view.panel.CoordinateMousePositionPanel', {
.split(':')[1];
var filtered = Ext.Array.filter(proj4jObjects, function(obj) {
// Loose equality check since code could be a string or an int
return obj.code == mapCode;
return obj.code === mapCode;
});
me.getViewModel().setData({
srsName: filtered ? filtered[0].name : ''
Expand Down
35 changes: 21 additions & 14 deletions test/spec/util/Projection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ describe('BasiGX.util.Projection', function() {

describe('Advanced', function() {
var projectionsStore = BasiGX.store.Projections;
var xhr;
var fakeRequest;
var requests = [];
beforeEach(function() {
xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (xhr) {
fakeRequest = sinon.useFakeXMLHttpRequest();
fakeRequest.onCreate = function (xhr) {
requests.push(xhr);
}
};
});

afterEach(function() {
projectionsStore.removeAll();
xhr.restore();
fakeRequest.restore();
requests = [];
});

Expand All @@ -40,21 +40,26 @@ describe('BasiGX.util.Projection', function() {
expect(requests[1].url).to.contain('https://epsg.io/?q=3857');

// respond with dummy data so the promise resolves
requests[0].respond(200, { "Content-Type": "application/json" }, '{ "results": [{ "code": 4326 }] }');
requests[1].respond(200, { "Content-Type": "application/json" }, '{ "results": [{ "code": 3857 }] }');
requests[0].respond(200, {
"Content-Type": "application/json"
}, '{ "results": [{ "code": 4326 }] }');
requests[1].respond(200, {
"Content-Type": "application/json"
}, '{ "results": [{ "code": 3857 }] }');
return promise;
});

it('uses data from the projections store when available', function() {
// pre-load two definitions
projectionsStore.loadData([{
code: 4326,
code: '4326',
name: 'WGS 84',
proj4: '+proj=longlat +datum=WGS84 +no_defs +type=crs',
unit: 'degree (supplier to define representation)',
unit: 'degree (supplier to define representation)'
}, {
code: 3857,
code: '3857',
name: 'WGS 84 / Pseudo-Mercator',
// eslint-disable-next-line max-len
proj4: '+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs',
unit: 'metre'
}]);
Expand All @@ -71,11 +76,13 @@ describe('BasiGX.util.Projection', function() {
expect(requests[0].url).to.contain('https://epsg.io/?q=29902');

// respond with dummy data so the promise resolves
requests[0].respond(200, { "Content-Type": "application/json" }, '{ "results": [{ "code": 29902 }] }');
requests[0].respond(200, {
"Content-Type": "application/json"
}, '{ "results": [{ "code": 29902 }] }');
return promise.then(function (data) {
expect(+data[0].code).to.be(4326);
expect(+data[1].code).to.be(3857);
expect(+data[2].code).to.be(29902);
expect(Number(data[0].code)).to.be(4326);
expect(Number(data[1].code)).to.be(3857);
expect(Number(data[2].code)).to.be(29902);
});
});
});
Expand Down

0 comments on commit 9a9dfba

Please sign in to comment.