Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce some utils #431

Merged
merged 11 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/util/Date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (c) 2019-present terrestris GmbH & Co. KG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Date utility.
*
* @class BasiGX.util.Date
*/
Ext.define('BasiGX.util.Date', {
inheritableStatics: {
/**
* Returns a `Date` instance where the date part is taken from the
* passed `anyDate`, and the time is set to `'23:59:59.999'`.
*
* @param {Date} anyDate The date to take the year, month and day from.
* @return {Date} A date with year, month and day from the input and
* the time set to `'23:59:59.999'`.
*/
latestTimeOfDay: function(anyDate) {
// clone to get the original year, month and day
var adjusted = Ext.Date.clone(anyDate);

// start with 00:00:00.000
adjusted = Ext.Date.clearTime(adjusted);

// add hours, minutes etc.
adjusted = Ext.Date.add(adjusted, Ext.Date.HOUR, 23);
adjusted = Ext.Date.add(adjusted, Ext.Date.MINUTE, 59);
adjusted = Ext.Date.add(adjusted, Ext.Date.SECOND, 59);
adjusted = Ext.Date.add(adjusted, Ext.Date.MILLI, 999);

return adjusted;
},

/**
* Tries to parse a passed date string into a `Date`-object. Does this
* by first checking the `Ext.Date.defaultFormat` (i18n-able). In a
* certain case (en-locale) another date format is checked.
*
* @param {String} dateStr A string representing a date, usually in
* the format `Ext.Date.defaultFormat`.
* @return {Date} The parsed date as `Date`-object.
*/
selectedDateStringToRealDate: function(dateStr) {
var format = Ext.Date.defaultFormat;
var parsed = Ext.Date.parse(dateStr, format);
if (!parsed && format === 'm/d/Y') {
// Needed for the english locale:
// Parsing failed, try it with 'm/d/y' and passed value
// TODO we need find the source of this oddish behaviour
parsed = Ext.Date.parse(dateStr, 'm/d/y');
}
return parsed;
}
}

});
5 changes: 0 additions & 5 deletions src/util/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*/
Ext.define('BasiGX.util.Filter', {

requires: 'BasiGX.util.SLD',

statics: {

/**
Expand Down Expand Up @@ -432,7 +430,4 @@ Ext.define('BasiGX.util.Filter', {
return spatialFilter;
}
}
}, function() {
BasiGX.util.SLD
.setStaticJsonixReferences(BasiGX.util.Filter);
});
74 changes: 74 additions & 0 deletions src/util/GeometryOperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright (c) 2019-present terrestris GmbH & Co. KG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Utility class containing static methods for geometry operations.
*
* @class BasiGX.util.GeometryOperations
*/
Ext.define('BasiGX.util.GeometryOperations', {

statics: {

/**
* The circle geometries are not supported by the WKT format, so it
* can't be used for modify iterations.

* Since OpenLayers still doesn't support it
* (s. also https://github.com/openlayers/ol3/issues/3777) we need to
* transform the drawn circle to the approximate regular polygon with
* given circle geometry.
*
* The passed collection is modified in place.
*
* @param {ol.Collection} features The collection of features to
* transform to features with a `ol.geom.Polygon` geometry. This
* collection is modified in place.
*/
translateCircleToPolygon: function(features) {
Ext.each(features.getArray(), function(f) {
if (f.getGeometry().getType() === "Circle") {
var geom = f.getGeometry();
var newGeom = new ol.geom.Polygon.fromCircle(geom);
var newFeat = new ol.Feature(newGeom);

features.remove(f);
features.insertAt(
features.getLength(),
newFeat
);
}
});
},

/**
* Computes circle feature radius depending on given center and the
* second coordinate as distance from the circle center to the vertices.
*
* @param {ol.Coordinate} start The start coordinates as array (lat/lon
* or x/y).
* @param {ol.Coordinate} end The end coordinates as array (lat/lon or
* x/y).
* @return {Number} The radius of a circle with center at `start` going
* through `end`.
*/
computeCircleRadius: function(start, end) {
var deltaX = end[0] - start[0];
var deltaY = end[1] - start[1];
return Math.sqrt((deltaX * deltaX) + (deltaY * deltaY));
}
}

});
83 changes: 83 additions & 0 deletions src/util/Jsonix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* Copyright (c) 2019-present terrestris GmbH & Co. KG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Common utility class containing static methods for determination,
* initialization and storing of Jsonix components.
*
* @class BasiGX.util.Jsonix
*/
Ext.define('BasiGX.util.Jsonix', {
inheritableStatics: {
jsonixContext: null,
marshaller: null,
unmarshaller: null,
possibleGlobals: [
'Jsonix',
'Filter_1_0_0',
'SLD_1_0_0',
'SMIL_2_0',
'SMIL_2_0_Language',
'GML_2_1_2',
'GML_3_1_1',
'XLink_1_0',
'WPS_1_0_0',
'OWS_1_1_0',
'WCS_1_1'
],
/**
* Create instances of Jsonix classes and make them accessible as static
* properties.
*
*/
setStaticJsonixReferences: function() {
var staticMe = BasiGX.util.Jsonix;
var availableGlobals = [];
Ext.each(staticMe.possibleGlobals, function(possible) {
if (!(possible in window)) {
Ext.Logger.warn(
'Possible global variable "' +
possible + '" not found. ' +
'This functionality will not be available!'
);
} else {
availableGlobals.push(window[possible]);
}
});
// create the objects…
var context = new Jsonix.Context(
availableGlobals, {
namespacePrefixes: {
'http://www.opengis.net/sld': 'sld',
"http://www.opengis.net/ogc": "ogc",
"http://www.opengis.net/gml": "gml",
"http://www.w3.org/2001/XMLSchema-instance": "xsi",
"http://www.w3.org/1999/xlink": "xlink",
"http://www.opengis.net/ows/1.1": "ows",
"http://www.opengis.net/wps/1.0.0": "wps",
"http://www.opengis.net/wcs/1.1.1": "wcs"
}
});
var marshaller = context.createMarshaller();
var unmarshaller = context.createUnmarshaller();
// … and store them in the static variables.
staticMe.jsonixContext = context;
staticMe.marshaller = marshaller;
staticMe.unmarshaller = unmarshaller;
}
}
}, function() {
BasiGX.util.Jsonix.setStaticJsonixReferences();
});
121 changes: 121 additions & 0 deletions src/util/MouseCoordinates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* Copyright (c) 2019-present terrestris GmbH & Co. KG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Utility class containing a static method for rendering a mouse coordinates
* formatted depending on currently set application projection.
*
* @class BasiGX.util.MouseCoordinates
*/
Ext.define('BasiGX.util.MouseCoordinates', {

statics: {
enNorth: 'N',
enEast: 'E',
enSouth: 'S',
enWest: 'W',
/* begin i18n */
mousePositionLabel: '',
dspNorth: '',
dspEast: '',
dspSouth: '',
dspWest: '',
/* end i18n */

/**
* Renderer for mouse coordinate string representation. The format
* depends on configured projection of the application. If some metric
* projection (e.g. EPSG:25832 or EPSG:3857) are used, coordinates are
* shown in meter, otherwise (e.g. EPSG:4326) degrees units are used.
*
* @param {Array<Number>} coord Current coordinate pair to be shown.
* @param {Array<Number>} hideProjectionName Whether the prefix
* containing current projection name should be hidden or not.
* @return {String} Formatted mouse position label.
*/
mouseCoordinateRenderer: function(coord, hideProjectionName) {
var staticMe = BasiGX.util.MouseCoordinates;
var map = BasiGX.util.Map.getMapComponent().getMap();
var proj = map.getView().getProjection();
var unitsAreMetric = proj.getUnits() === 'm';

// for some reason, in case of metric units, the fraction digits
// seem not to be correct/irritating in case of metric units...
var fractionDigits = unitsAreMetric === true ? 0 : 3;

var decimal = ol.coordinate.format(
coord, '{x} / {y}', fractionDigits
);
var decimalSeparator = Ext.util.Format.decimalSeparator;

if (decimalSeparator !== '.') {
// replace the point as decimal separator with locale one
decimal = decimal.replace(/\./g, decimalSeparator);
}

if (!unitsAreMetric) {
var hdms = ol.coordinate.toStringHDMS(coord);
// handle possibly different abbreviations for north, east, etc.
if (staticMe.enNorth !== staticMe.dspNorth) {
hdms = hdms.replace(
new RegExp(staticMe.enNorth, 'g'),
staticMe.dspNorth
);
}
if (staticMe.enEast !== staticMe.dspEast) {
hdms = hdms.replace(
new RegExp(staticMe.enEast, 'g'),
staticMe.dspEast
);
}
if (staticMe.enSouth !== staticMe.dspSouth) {
hdms = hdms.replace(
new RegExp(staticMe.enSouth, 'g'),
staticMe.dspSouth
);
}
if (staticMe.enWest !== staticMe.dspWest) {
hdms = hdms.replace(
new RegExp(staticMe.enWest, 'g'),
staticMe.dspWest
);
}
if (hideProjectionName) {
return Ext.String.format(
"{0}<br/>({1})",
decimal, hdms
);
} else {
return Ext.String.format(
"{0} ({1}): {2}<br/>({3})",
staticMe.mousePositionLabel, proj.getCode(),
decimal, hdms
);
}
} else {
if (hideProjectionName) {
return Ext.String.format("{0} (m)", decimal);
} else {
return Ext.String.format(
"{0} ({1}):<br/>{2} (m)",
staticMe.mousePositionLabel, proj.getCode(),
decimal
);
}

}
}
}
});
Loading