Replace instanceof checks with other logic

This commit is contained in:
ahocevar
2018-10-05 16:16:51 +02:00
parent 96e99e481e
commit 9163558511
40 changed files with 232 additions and 268 deletions
+12 -13
View File
@@ -1,7 +1,6 @@
/**
* @module ol/format/Feature
*/
import Geometry from '../geom/Geometry.js';
import {assign} from '../obj.js';
import {get as getProjection, equivalent as equivalentProjection, transformExtent} from '../proj.js';
@@ -151,7 +150,7 @@ class FeatureFormat {
* @abstract
* @param {Document|Node|Object|string} source Source.
* @param {ReadOptions=} opt_options Read options.
* @return {Geometry} Geometry.
* @return {import("../geom/Geometry.js").default} Geometry.
*/
readGeometry(source, opt_options) {}
@@ -188,7 +187,7 @@ class FeatureFormat {
* Write a single geometry in this format.
*
* @abstract
* @param {Geometry} geometry Geometry.
* @param {import("../geom/Geometry.js").default} geometry Geometry.
* @param {WriteOptions=} opt_options Write options.
* @return {string} Result.
*/
@@ -198,10 +197,10 @@ class FeatureFormat {
export default FeatureFormat;
/**
* @param {Geometry|import("../extent.js").Extent} geometry Geometry.
* @param {import("../geom/Geometry.js").default|import("../extent.js").Extent} geometry Geometry.
* @param {boolean} write Set to true for writing, false for reading.
* @param {(WriteOptions|ReadOptions)=} opt_options Options.
* @return {Geometry|import("../extent.js").Extent} Transformed geometry.
* @return {import("../geom/Geometry.js").default|import("../extent.js").Extent} Transformed geometry.
*/
export function transformWithOptions(geometry, write, opt_options) {
const featureProjection = opt_options ?
@@ -209,28 +208,28 @@ export function transformWithOptions(geometry, write, opt_options) {
const dataProjection = opt_options ?
getProjection(opt_options.dataProjection) : null;
/**
* @type {Geometry|import("../extent.js").Extent}
* @type {import("../geom/Geometry.js").default|import("../extent.js").Extent}
*/
let transformed;
if (featureProjection && dataProjection &&
!equivalentProjection(featureProjection, dataProjection)) {
if (geometry instanceof Geometry) {
transformed = (write ? geometry.clone() : geometry).transform(
write ? featureProjection : dataProjection,
write ? dataProjection : featureProjection);
} else {
if (Array.isArray(geometry)) {
// FIXME this is necessary because GML treats extents
// as geometries
transformed = transformExtent(
geometry,
dataProjection,
featureProjection);
} else {
transformed = (write ? /** @type {import("../geom/Geometry").default} */ (geometry).clone() : geometry).transform(
write ? featureProjection : dataProjection,
write ? dataProjection : featureProjection);
}
} else {
transformed = geometry;
}
if (write && opt_options && /** @type {WriteOptions} */ (opt_options).decimals !== undefined &&
transformed instanceof Geometry) {
!Array.isArray(transformed)) {
const power = Math.pow(10, /** @type {WriteOptions} */ (opt_options).decimals);
// if decimals option on write, round each coordinate appropriately
/**
@@ -244,7 +243,7 @@ export function transformWithOptions(geometry, write, opt_options) {
return coordinates;
};
if (transformed === geometry) {
transformed = transformed.clone();
transformed = /** @type {import("../geom/Geometry").default} */ (geometry).clone();
}
transformed.applyTransform(transform);
}
+1 -2
View File
@@ -5,7 +5,6 @@ import {createOrUpdate} from '../extent.js';
import {transformWithOptions} from '../format/Feature.js';
import GMLBase, {GMLNS} from '../format/GMLBase.js';
import {writeStringTextNode} from '../format/xsd.js';
import Geometry from '../geom/Geometry.js';
import {assign} from '../obj.js';
import {get as getProjection, transformExtent} from '../proj.js';
import {createElementNS, getAllTextContent, makeArrayPusher, makeChildAppender,
@@ -196,7 +195,7 @@ class GML2 extends GMLBase {
if (value !== null) {
keys.push(key);
values.push(value);
if (key == geometryName || value instanceof Geometry) {
if (key == geometryName || typeof /** @type {?} */ (value).getSimplifiedGeometry === 'function') {
if (!(key in context.serializers[featureNS])) {
context.serializers[featureNS][key] = makeChildAppender(
this.writeGeometryElement, this);
+1 -2
View File
@@ -6,7 +6,6 @@ import {createOrUpdate} from '../extent.js';
import {transformWithOptions} from '../format/Feature.js';
import GMLBase, {GMLNS} from '../format/GMLBase.js';
import {readNonNegativeIntegerString, writeStringTextNode} from '../format/xsd.js';
import Geometry from '../geom/Geometry.js';
import GeometryLayout from '../geom/GeometryLayout.js';
import LineString from '../geom/LineString.js';
import MultiLineString from '../geom/MultiLineString.js';
@@ -774,7 +773,7 @@ class GML3 extends GMLBase {
if (value !== null) {
keys.push(key);
values.push(value);
if (key == geometryName || value instanceof Geometry) {
if (key == geometryName || typeof /** @type {?} */ (value).getSimplifiedGeometry === 'function') {
if (!(key in context.serializers[featureNS])) {
context.serializers[featureNS][key] = makeChildAppender(
this.writeGeometryElement, this);
+4 -3
View File
@@ -7,6 +7,7 @@ import {transformWithOptions} from '../format/Feature.js';
import XMLFeature from '../format/XMLFeature.js';
import {readString, readDecimal, readNonNegativeInteger, readDateTime, writeStringTextNode, writeNonNegativeIntegerTextNode, writeDecimalTextNode, writeDateTimeTextNode} from '../format/xsd.js';
import GeometryLayout from '../geom/GeometryLayout.js';
import GeometryType from '../geom/GeometryType.js';
import LineString from '../geom/LineString.js';
import MultiLineString from '../geom/MultiLineString.js';
import Point from '../geom/Point.js';
@@ -782,7 +783,7 @@ function writeRte(node, feature, objectStack) {
const context = {node: node};
context['properties'] = properties;
const geometry = feature.getGeometry();
if (geometry instanceof LineString) {
if (geometry.getType() == GeometryType.LINE_STRING) {
const lineString = /** @type {LineString} */ (transformWithOptions(geometry, true, options));
context['geometryLayout'] = lineString.getLayout();
properties['rtept'] = lineString.getCoordinates();
@@ -808,7 +809,7 @@ function writeTrk(node, feature, objectStack) {
const context = {node: node};
context['properties'] = properties;
const geometry = feature.getGeometry();
if (geometry instanceof MultiLineString) {
if (geometry.getType() == GeometryType.MULTI_LINE_STRING) {
const multiLineString = /** @type {MultiLineString} */ (transformWithOptions(geometry, true, options));
properties['trkseg'] = multiLineString.getLineStrings();
}
@@ -847,7 +848,7 @@ function writeWpt(node, feature, objectStack) {
const context = objectStack[objectStack.length - 1];
context['properties'] = feature.getProperties();
const geometry = feature.getGeometry();
if (geometry instanceof Point) {
if (geometry.getType() == GeometryType.POINT) {
const point = /** @type {Point} */ (transformWithOptions(geometry, true, options));
context['geometryLayout'] = point.getLayout();
writeWptType(node, point.getCoordinates(), objectStack);
+21 -21
View File
@@ -26,7 +26,7 @@ import IconOrigin from '../style/IconOrigin.js';
import Stroke from '../style/Stroke.js';
import Style from '../style/Style.js';
import Text from '../style/Text.js';
import {createElementNS, getAllTextContent, isDocument, isNode, makeArrayExtender,
import {createElementNS, getAllTextContent, isDocument, makeArrayExtender,
makeArrayPusher, makeChildAppender, makeObjectPropertySetter,
makeReplacer, makeSequence, makeSimpleNodeFactory, makeStructureNS,
OBJECT_PROPERTY_NODE_FACTORY, parse, parseNode, pushParseAndPop,
@@ -639,15 +639,15 @@ class KML extends XMLFeature {
* @api
*/
readName(source) {
if (isDocument(source)) {
return this.readNameFromDocument(/** @type {Document} */ (source));
} else if (isNode(source)) {
return this.readNameFromNode(/** @type {Element} */ (source));
if (!source) {
return undefined;
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readNameFromDocument(doc);
} else if (isDocument(source)) {
return this.readNameFromDocument(/** @type {Document} */ (source));
} else {
return undefined;
return this.readNameFromNode(/** @type {Element} */ (source));
}
}
@@ -703,15 +703,15 @@ class KML extends XMLFeature {
*/
readNetworkLinks(source) {
const networkLinks = [];
if (isDocument(source)) {
extend(networkLinks, this.readNetworkLinksFromDocument(
/** @type {Document} */ (source)));
} else if (isNode(source)) {
extend(networkLinks, this.readNetworkLinksFromNode(
/** @type {Element} */ (source)));
} else if (typeof source === 'string') {
if (typeof source === 'string') {
const doc = parse(source);
extend(networkLinks, this.readNetworkLinksFromDocument(doc));
} else if (isDocument(source)) {
extend(networkLinks, this.readNetworkLinksFromDocument(
/** @type {Document} */ (source)));
} else {
extend(networkLinks, this.readNetworkLinksFromNode(
/** @type {Element} */ (source)));
}
return networkLinks;
}
@@ -765,15 +765,15 @@ class KML extends XMLFeature {
*/
readRegion(source) {
const regions = [];
if (isDocument(source)) {
extend(regions, this.readRegionFromDocument(
/** @type {Document} */ (source)));
} else if (isNode(source)) {
extend(regions, this.readRegionFromNode(
/** @type {Element} */ (source)));
} else if (typeof source === 'string') {
if (typeof source === 'string') {
const doc = parse(source);
extend(regions, this.readRegionFromDocument(doc));
} else if (isDocument(source)) {
extend(regions, this.readRegionFromDocument(
/** @type {Document} */ (source)));
} else {
extend(regions, this.readRegionFromNode(
/** @type {Element} */ (source)));
}
return regions;
}
@@ -2897,7 +2897,7 @@ function writeStyle(node, style, objectStack) {
const strokeStyle = style.getStroke();
const imageStyle = style.getImage();
const textStyle = style.getText();
if (imageStyle instanceof Icon) {
if (imageStyle && typeof /** @type {?} */ (imageStyle).getSrc === 'function') {
properties['IconStyle'] = imageStyle;
}
if (textStyle) {
+16 -17
View File
@@ -8,10 +8,9 @@ import GMLBase, {GMLNS} from '../format/GMLBase.js';
import {and as andFilter, bbox as bboxFilter} from '../format/filter.js';
import XMLFeature from '../format/XMLFeature.js';
import {readNonNegativeIntegerString, readNonNegativeInteger, writeStringTextNode} from '../format/xsd.js';
import Geometry from '../geom/Geometry.js';
import {assign} from '../obj.js';
import {get as getProjection} from '../proj.js';
import {createElementNS, isDocument, isNode, makeArrayPusher, makeChildAppender,
import {createElementNS, isDocument, makeArrayPusher, makeChildAppender,
makeObjectPropertySetter, makeSimpleNodeFactory, parse, parseNode,
pushParseAndPop, pushSerializeAndPop, XML_SCHEMA_INSTANCE_URI} from '../xml.js';
@@ -288,16 +287,16 @@ class WFS extends XMLFeature {
* @api
*/
readTransactionResponse(source) {
if (isDocument(source)) {
return this.readTransactionResponseFromDocument(
/** @type {Document} */ (source));
} else if (isNode(source)) {
return this.readTransactionResponseFromNode(/** @type {Element} */ (source));
if (!source) {
return undefined;
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readTransactionResponseFromDocument(doc);
} else if (isDocument(source)) {
return this.readTransactionResponseFromDocument(
/** @type {Document} */ (source));
} else {
return undefined;
return this.readTransactionResponseFromNode(/** @type {Element} */ (source));
}
}
@@ -310,17 +309,17 @@ class WFS extends XMLFeature {
* @api
*/
readFeatureCollectionMetadata(source) {
if (isDocument(source)) {
return this.readFeatureCollectionMetadataFromDocument(
/** @type {Document} */ (source));
} else if (isNode(source)) {
return this.readFeatureCollectionMetadataFromNode(
/** @type {Element} */ (source));
if (!source) {
return undefined;
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readFeatureCollectionMetadataFromDocument(doc);
} else if (isDocument(source)) {
return this.readFeatureCollectionMetadataFromDocument(
/** @type {Document} */ (source));
} else {
return undefined;
return this.readFeatureCollectionMetadataFromNode(
/** @type {Element} */ (source));
}
}
@@ -694,7 +693,7 @@ function writeUpdate(node, feature, objectStack) {
const value = feature.get(keys[i]);
if (value !== undefined) {
let name = keys[i];
if (value instanceof Geometry) {
if (value && typeof /** @type {?} */ (value).getSimplifiedGeometry === 'function') {
name = geometryName;
}
values.push({name: name, value: value});
@@ -725,7 +724,7 @@ function writeProperty(node, pair, objectStack) {
if (pair.value !== undefined && pair.value !== null) {
const value = createElementNS(WFSNS, 'Value');
node.appendChild(value);
if (pair.value instanceof Geometry) {
if (pair.value && typeof /** @type {?} */ (pair.value).getSimplifiedGeometry === 'function') {
if (gmlVersion === 2) {
GML2.prototype.writeGeometryElement(value,
pair.value, objectStack);
+4 -5
View File
@@ -13,7 +13,6 @@ import MultiPoint from '../geom/MultiPoint.js';
import MultiPolygon from '../geom/MultiPolygon.js';
import Point from '../geom/Point.js';
import Polygon from '../geom/Polygon.js';
import SimpleGeometry from '../geom/SimpleGeometry.js';
/**
@@ -823,7 +822,7 @@ function encodeMultiPolygonGeometry(geom) {
}
/**
* @param {SimpleGeometry} geom SimpleGeometry geometry.
* @param {import("../geom/SimpleGeometry.js").default} geom SimpleGeometry geometry.
* @return {string} Potential dimensional information for WKT type.
*/
function encodeGeometryLayout(geom) {
@@ -856,7 +855,7 @@ const GeometryEncoder = {
/**
* Encode a geometry as WKT.
* @param {import("../geom/Geometry.js").default} geom The geometry to encode.
* @param {!import("../geom/Geometry.js").default} geom The geometry to encode.
* @return {string} WKT string for the geometry.
*/
function encode(geom) {
@@ -864,8 +863,8 @@ function encode(geom) {
const geometryEncoder = GeometryEncoder[type];
const enc = geometryEncoder(geom);
type = type.toUpperCase();
if (geom instanceof SimpleGeometry) {
const dimInfo = encodeGeometryLayout(geom);
if (typeof /** @type {?} */ (geom).getFlatCoordinates === 'function') {
const dimInfo = encodeGeometryLayout(/** @type {import("../geom/SimpleGeometry.js").default} */ (geom));
if (dimInfo.length > 0) {
type += ' ' + dimInfo;
}
+6 -6
View File
@@ -1,7 +1,7 @@
/**
* @module ol/format/XML
*/
import {isDocument, isNode, parse} from '../xml.js';
import {isDocument, parse} from '../xml.js';
/**
* @classdesc
@@ -18,15 +18,15 @@ class XML {
* @api
*/
read(source) {
if (isDocument(source)) {
return this.readFromDocument(/** @type {Document} */ (source));
} else if (isNode(source)) {
return this.readFromNode(/** @type {Element} */ (source));
if (!source) {
return null;
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readFromDocument(doc);
} else if (isDocument(source)) {
return this.readFromDocument(/** @type {Document} */ (source));
} else {
return null;
return this.readFromNode(/** @type {Element} */ (source));
}
}
+23 -23
View File
@@ -4,7 +4,7 @@
import {extend} from '../array.js';
import FeatureFormat from '../format/Feature.js';
import FormatType from '../format/FormatType.js';
import {isDocument, isNode, parse} from '../xml.js';
import {isDocument, parse} from '../xml.js';
/**
* @classdesc
@@ -41,15 +41,15 @@ class XMLFeature extends FeatureFormat {
* @api
*/
readFeature(source, opt_options) {
if (isDocument(source)) {
return this.readFeatureFromDocument(/** @type {Document} */ (source), opt_options);
} else if (isNode(source)) {
return this.readFeatureFromNode(/** @type {Node} */ (source), opt_options);
if (!source) {
return null;
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readFeatureFromDocument(doc, opt_options);
} else if (isDocument(source)) {
return this.readFeatureFromDocument(/** @type {Document} */ (source), opt_options);
} else {
return null;
return this.readFeatureFromNode(/** @type {Node} */ (source), opt_options);
}
}
@@ -85,16 +85,16 @@ class XMLFeature extends FeatureFormat {
* @api
*/
readFeatures(source, opt_options) {
if (isDocument(source)) {
return this.readFeaturesFromDocument(
/** @type {Document} */ (source), opt_options);
} else if (isNode(source)) {
return this.readFeaturesFromNode(/** @type {Node} */ (source), opt_options);
if (!source) {
return [];
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readFeaturesFromDocument(doc, opt_options);
} else if (isDocument(source)) {
return this.readFeaturesFromDocument(
/** @type {Document} */ (source), opt_options);
} else {
return [];
return this.readFeaturesFromNode(/** @type {Node} */ (source), opt_options);
}
}
@@ -128,16 +128,16 @@ class XMLFeature extends FeatureFormat {
* @inheritDoc
*/
readGeometry(source, opt_options) {
if (isDocument(source)) {
return this.readGeometryFromDocument(
/** @type {Document} */ (source), opt_options);
} else if (isNode(source)) {
return this.readGeometryFromNode(/** @type {Node} */ (source), opt_options);
if (!source) {
return null;
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readGeometryFromDocument(doc, opt_options);
} else if (isDocument(source)) {
return this.readGeometryFromDocument(
/** @type {Document} */ (source), opt_options);
} else {
return null;
return this.readGeometryFromNode(/** @type {Node} */ (source), opt_options);
}
}
@@ -169,15 +169,15 @@ class XMLFeature extends FeatureFormat {
* @api
*/
readProjection(source) {
if (isDocument(source)) {
return this.readProjectionFromDocument(/** @type {Document} */ (source));
} else if (isNode(source)) {
return this.readProjectionFromNode(/** @type {Node} */ (source));
if (!source) {
return null;
} else if (typeof source === 'string') {
const doc = parse(source);
return this.readProjectionFromDocument(doc);
} else if (isDocument(source)) {
return this.readProjectionFromDocument(/** @type {Document} */ (source));
} else {
return null;
return this.readProjectionFromNode(/** @type {Node} */ (source));
}
}