Remove unnecessary type casts

This commit is contained in:
Tim Schaub
2018-09-11 09:02:33 -06:00
parent 5b00231d7a
commit 39c31e62a3
+33 -30
View File
@@ -1,8 +1,6 @@
/** /**
* @module ol/format/GeoJSON * @module ol/format/GeoJSON
*/ */
// TODO: serialize dataProjection as crs member when writing
// see https://github.com/openlayers/openlayers/issues/2078
import {assert} from '../asserts.js'; import {assert} from '../asserts.js';
import Feature from '../Feature.js'; import Feature from '../Feature.js';
@@ -102,10 +100,11 @@ class GeoJSON extends JSONFeature {
if (object['type'] === 'Feature') { if (object['type'] === 'Feature') {
geoJSONFeature = /** @type {GeoJSONFeature} */ (object); geoJSONFeature = /** @type {GeoJSONFeature} */ (object);
} else { } else {
geoJSONFeature = /** @type {GeoJSONFeature} */ ({ geoJSONFeature = {
type: 'Feature', 'type': 'Feature',
geometry: /** @type {GeoJSONGeometry} */ (object) 'geometry': /** @type {GeoJSONGeometry} */ (object),
}); 'properties': null
};
} }
const geometry = readGeometry(geoJSONFeature['geometry'], opt_options); const geometry = readGeometry(geoJSONFeature['geometry'], opt_options);
@@ -116,10 +115,12 @@ class GeoJSON extends JSONFeature {
feature.setGeometryName(geoJSONFeature['geometry_name']); feature.setGeometryName(geoJSONFeature['geometry_name']);
} }
feature.setGeometry(geometry); feature.setGeometry(geometry);
if ('id' in geoJSONFeature) { if ('id' in geoJSONFeature) {
feature.setId(geoJSONFeature['id']); feature.setId(geoJSONFeature['id']);
} }
if ('properties' in geoJSONFeature) {
if (geoJSONFeature['properties']) {
feature.setProperties(geoJSONFeature['properties']); feature.setProperties(geoJSONFeature['properties']);
} }
return feature; return feature;
@@ -184,25 +185,27 @@ class GeoJSON extends JSONFeature {
writeFeatureObject(feature, opt_options) { writeFeatureObject(feature, opt_options) {
opt_options = this.adaptOptions(opt_options); opt_options = this.adaptOptions(opt_options);
const object = /** @type {GeoJSONFeature} */ ({ /** @type {GeoJSONFeature} */
'type': 'Feature' const object = {
}); 'type': 'Feature',
geometry: null,
properties: null
};
const id = feature.getId(); const id = feature.getId();
if (id !== undefined) { if (id !== undefined) {
object.id = id; object.id = id;
} }
const geometry = feature.getGeometry(); const geometry = feature.getGeometry();
if (geometry) { if (geometry) {
object.geometry = writeGeometry(geometry, opt_options); object.geometry = writeGeometry(geometry, opt_options);
} else {
object.geometry = null;
} }
const properties = feature.getProperties(); const properties = feature.getProperties();
delete properties[feature.getGeometryName()]; delete properties[feature.getGeometryName()];
if (!isEmpty(properties)) { if (!isEmpty(properties)) {
object.properties = properties; object.properties = properties;
} else {
object.properties = null;
} }
return object; return object;
} }
@@ -222,10 +225,10 @@ class GeoJSON extends JSONFeature {
for (let i = 0, ii = features.length; i < ii; ++i) { for (let i = 0, ii = features.length; i < ii; ++i) {
objects.push(this.writeFeatureObject(features[i], opt_options)); objects.push(this.writeFeatureObject(features[i], opt_options));
} }
return /** @type {GeoJSONFeatureCollection} */ ({ return {
type: 'FeatureCollection', type: 'FeatureCollection',
features: objects features: objects
}); };
} }
/** /**
@@ -432,10 +435,10 @@ function writeGeometryCollectionGeometry(geometry, opt_options) {
delete options.featureProjection; delete options.featureProjection;
return writeGeometry(geometry, options); return writeGeometry(geometry, options);
}); });
return /** @type {GeoJSONGeometryCollection} */ ({ return {
type: 'GeometryCollection', type: 'GeometryCollection',
geometries: geometries geometries: geometries
}); };
} }
@@ -445,10 +448,10 @@ function writeGeometryCollectionGeometry(geometry, opt_options) {
* @return {GeoJSONGeometry} GeoJSON geometry. * @return {GeoJSONGeometry} GeoJSON geometry.
*/ */
function writeLineStringGeometry(geometry, opt_options) { function writeLineStringGeometry(geometry, opt_options) {
return /** @type {GeoJSONGeometry} */ ({ return {
type: 'LineString', type: 'LineString',
coordinates: geometry.getCoordinates() coordinates: geometry.getCoordinates()
}); };
} }
@@ -458,10 +461,10 @@ function writeLineStringGeometry(geometry, opt_options) {
* @return {GeoJSONGeometry} GeoJSON geometry. * @return {GeoJSONGeometry} GeoJSON geometry.
*/ */
function writeMultiLineStringGeometry(geometry, opt_options) { function writeMultiLineStringGeometry(geometry, opt_options) {
return /** @type {GeoJSONGeometry} */ ({ return {
type: 'MultiLineString', type: 'MultiLineString',
coordinates: geometry.getCoordinates() coordinates: geometry.getCoordinates()
}); };
} }
@@ -471,10 +474,10 @@ function writeMultiLineStringGeometry(geometry, opt_options) {
* @return {GeoJSONGeometry} GeoJSON geometry. * @return {GeoJSONGeometry} GeoJSON geometry.
*/ */
function writeMultiPointGeometry(geometry, opt_options) { function writeMultiPointGeometry(geometry, opt_options) {
return /** @type {GeoJSONGeometry} */ ({ return {
type: 'MultiPoint', type: 'MultiPoint',
coordinates: geometry.getCoordinates() coordinates: geometry.getCoordinates()
}); };
} }
@@ -488,10 +491,10 @@ function writeMultiPolygonGeometry(geometry, opt_options) {
if (opt_options) { if (opt_options) {
right = opt_options.rightHanded; right = opt_options.rightHanded;
} }
return /** @type {GeoJSONGeometry} */ ({ return {
type: 'MultiPolygon', type: 'MultiPolygon',
coordinates: geometry.getCoordinates(right) coordinates: geometry.getCoordinates(right)
}); };
} }
@@ -501,10 +504,10 @@ function writeMultiPolygonGeometry(geometry, opt_options) {
* @return {GeoJSONGeometry} GeoJSON geometry. * @return {GeoJSONGeometry} GeoJSON geometry.
*/ */
function writePointGeometry(geometry, opt_options) { function writePointGeometry(geometry, opt_options) {
return /** @type {GeoJSONGeometry} */ ({ return {
type: 'Point', type: 'Point',
coordinates: geometry.getCoordinates() coordinates: geometry.getCoordinates()
}); };
} }
@@ -518,10 +521,10 @@ function writePolygonGeometry(geometry, opt_options) {
if (opt_options) { if (opt_options) {
right = opt_options.rightHanded; right = opt_options.rightHanded;
} }
return /** @type {GeoJSONGeometry} */ ({ return {
type: 'Polygon', type: 'Polygon',
coordinates: geometry.getCoordinates(right) coordinates: geometry.getCoordinates(right)
}); };
} }