UNPKG

c8y-openlayer

Version:

This module is designed to help integrate Openlayer with Cumulocity IoT

220 lines (191 loc) 6.2 kB
import _ol_geom_Geometry_ from '../geom/geometry.js'; import _ol_obj_ from '../obj.js'; import _ol_proj_ from '../proj.js'; /** * @classdesc * Abstract base class; normally only used for creating subclasses and not * instantiated in apps. * Base class for feature formats. * {ol.format.Feature} subclasses provide the ability to decode and encode * {@link ol.Feature} objects from a variety of commonly used geospatial * file formats. See the documentation for each format for more details. * * @constructor * @abstract * @api */ var _ol_format_Feature_ = function() { /** * @protected * @type {ol.proj.Projection} */ this.defaultDataProjection = null; /** * @protected * @type {ol.proj.Projection} */ this.defaultFeatureProjection = null; }; /** * Adds the data projection to the read options. * @param {Document|Node|Object|string} source Source. * @param {olx.format.ReadOptions=} opt_options Options. * @return {olx.format.ReadOptions|undefined} Options. * @protected */ _ol_format_Feature_.prototype.getReadOptions = function(source, opt_options) { var options; if (opt_options) { options = { dataProjection: opt_options.dataProjection ? opt_options.dataProjection : this.readProjection(source), featureProjection: opt_options.featureProjection }; } return this.adaptOptions(options); }; /** * Sets the `defaultDataProjection` on the options, if no `dataProjection` * is set. * @param {olx.format.WriteOptions|olx.format.ReadOptions|undefined} options * Options. * @protected * @return {olx.format.WriteOptions|olx.format.ReadOptions|undefined} * Updated options. */ _ol_format_Feature_.prototype.adaptOptions = function(options) { return _ol_obj_.assign({ dataProjection: this.defaultDataProjection, featureProjection: this.defaultFeatureProjection }, options); }; /** * Get the extent from the source of the last {@link readFeatures} call. * @return {ol.Extent} Tile extent. */ _ol_format_Feature_.prototype.getLastExtent = function() { return null; }; /** * @abstract * @return {ol.format.FormatType} Format. */ _ol_format_Feature_.prototype.getType = function() {}; /** * Read a single feature from a source. * * @abstract * @param {Document|Node|Object|string} source Source. * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.Feature} Feature. */ _ol_format_Feature_.prototype.readFeature = function(source, opt_options) {}; /** * Read all features from a source. * * @abstract * @param {Document|Node|ArrayBuffer|Object|string} source Source. * @param {olx.format.ReadOptions=} opt_options Read options. * @return {Array.<ol.Feature>} Features. */ _ol_format_Feature_.prototype.readFeatures = function(source, opt_options) {}; /** * Read a single geometry from a source. * * @abstract * @param {Document|Node|Object|string} source Source. * @param {olx.format.ReadOptions=} opt_options Read options. * @return {ol.geom.Geometry} Geometry. */ _ol_format_Feature_.prototype.readGeometry = function(source, opt_options) {}; /** * Read the projection from a source. * * @abstract * @param {Document|Node|Object|string} source Source. * @return {ol.proj.Projection} Projection. */ _ol_format_Feature_.prototype.readProjection = function(source) {}; /** * Encode a feature in this format. * * @abstract * @param {ol.Feature} feature Feature. * @param {olx.format.WriteOptions=} opt_options Write options. * @return {string} Result. */ _ol_format_Feature_.prototype.writeFeature = function(feature, opt_options) {}; /** * Encode an array of features in this format. * * @abstract * @param {Array.<ol.Feature>} features Features. * @param {olx.format.WriteOptions=} opt_options Write options. * @return {string} Result. */ _ol_format_Feature_.prototype.writeFeatures = function(features, opt_options) {}; /** * Write a single geometry in this format. * * @abstract * @param {ol.geom.Geometry} geometry Geometry. * @param {olx.format.WriteOptions=} opt_options Write options. * @return {string} Result. */ _ol_format_Feature_.prototype.writeGeometry = function(geometry, opt_options) {}; /** * @param {ol.geom.Geometry|ol.Extent} geometry Geometry. * @param {boolean} write Set to true for writing, false for reading. * @param {(olx.format.WriteOptions|olx.format.ReadOptions)=} opt_options * Options. * @return {ol.geom.Geometry|ol.Extent} Transformed geometry. * @protected */ _ol_format_Feature_.transformWithOptions = function( geometry, write, opt_options) { var featureProjection = opt_options ? _ol_proj_.get(opt_options.featureProjection) : null; var dataProjection = opt_options ? _ol_proj_.get(opt_options.dataProjection) : null; /** * @type {ol.geom.Geometry|ol.Extent} */ var transformed; if (featureProjection && dataProjection && !_ol_proj_.equivalent(featureProjection, dataProjection)) { if (geometry instanceof _ol_geom_Geometry_) { transformed = (write ? geometry.clone() : geometry).transform( write ? featureProjection : dataProjection, write ? dataProjection : featureProjection); } else { // FIXME this is necessary because ol.format.GML treats extents // as geometries transformed = _ol_proj_.transformExtent( geometry, dataProjection, featureProjection); } } else { transformed = geometry; } if (write && opt_options && opt_options.decimals !== undefined) { var power = Math.pow(10, opt_options.decimals); // if decimals option on write, round each coordinate appropriately /** * @param {Array.<number>} coordinates Coordinates. * @return {Array.<number>} Transformed coordinates. */ var transform = function(coordinates) { for (var i = 0, ii = coordinates.length; i < ii; ++i) { coordinates[i] = Math.round(coordinates[i] * power) / power; } return coordinates; }; if (transformed === geometry) { transformed = transformed.clone(); } transformed.applyTransform(transform); } return transformed; }; export default _ol_format_Feature_;