Skip to content

Commit 3e60ce8

Browse files
committed
【fix】openlayers geometry相关支持geojson格式
1 parent 9ea573e commit 3e60ce8

File tree

13 files changed

+196
-45
lines changed

13 files changed

+196
-45
lines changed

src/common/format/GeoJSON.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,58 @@ export class GeoJSON extends JSONFormat {
617617
}
618618
return valid;
619619
}
620-
620+
/**
621+
* @static
622+
* @version 12.1.0
623+
* @function GeoJSONFormat.isGeoJSON
624+
* @description 判断一个对象是否是合法的 GeoJSON 对象。
625+
* @param {Object} obj - 待判断的对象。
626+
* @returns {boolean} 是否是合法的 GeoJSON 对象。
627+
*/
628+
static isGeoJSON(obj) {
629+
if (typeof obj !== 'object' || obj === null){
630+
return false;
631+
}
632+
const validTypes = [
633+
'Point', 'MultiPoint', 'LineString', 'MultiLineString',
634+
'Polygon', 'MultiPolygon', 'GeometryCollection',
635+
'Feature', 'FeatureCollection'
636+
];
637+
return validTypes.includes(obj.type);
638+
}
639+
/**
640+
* @static
641+
* @version 12.1.0
642+
* @function GeoJSONFormat.getGeoJSONType
643+
* @description 判断一个对象是否是合法的 GeoJSON 对象,如果不合法返回空,如果是合法的 GeoJSON 对象返回 FeatureCollection、Feature 或 Geometry。
644+
* @param {Object} obj - 待判断的对象。
645+
* @returns {string|null} 失败返回 null,成功返回 "FeatureCollection"、"Feature" 或 "Geometry"。
646+
*/
647+
static getGeoJSONType(obj) {
648+
if (typeof obj !== 'object' || obj === null) {
649+
return null;
650+
}
651+
if (obj.type === 'FeatureCollection') {
652+
return Util.isArray(obj.features) ? 'FeatureCollection' : null;
653+
}
654+
if (obj.type === 'Feature') {
655+
if (!obj.hasOwnProperty('geometry')) {
656+
return null;
657+
}
658+
return 'Feature';
659+
}
660+
const geometryTypes = [
661+
'Point', 'MultiPoint', 'LineString', 'MultiLineString',
662+
'Polygon', 'MultiPolygon', 'GeometryCollection'
663+
];
664+
if (geometryTypes.indexOf(obj.type) === -1) {
665+
return null;
666+
}
667+
if (obj.type === 'GeometryCollection') {
668+
return Util.isArray(obj.geometries) ? 'Geometry' : null;
669+
}
670+
return Util.isArray(obj.coordinates) ? 'Geometry' : null;
671+
}
621672
/**
622673
* @function GeoJSONFormat.prototype.parseFeature
623674
* @description 将一个 GeoJSON 中的 feature 转化成 {@link FeatureVector}> 对象。

src/common/iServer/index.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,57 @@ export { WebPrintingJobExportOptions };
565565
export { WebPrintingJobParameters };
566566
export { WebPrintingService };
567567
export { KnowledgeGraphEdgeParameter, KnowledgeGraphNodeParameter, KnowledgeGraphService };
568-
export { VideoFeature };
568+
export { VideoFeature };
569+
570+
571+
/**
572+
* @typedef {Object} ModuleFeature
573+
* @description 要素对象类型定义。<br>
574+
* @modulecondition openlayers:ol.Feature|GeoJSONObject
575+
* @modulecondition leaflet:GeoJSONObject
576+
* @modulecondition mapboxgl:GeoJSONObject
577+
* @modulecondition maplibregl:GeoJSONObject
578+
*/
579+
/**
580+
* @typedef {Object} ModuleGeometry
581+
* @description 几何对象类型定义。<br>
582+
* @modulecondition openlayers:ol.geom.Point|ol.geom.LineString|ol.geom.Polygon|GeoJSONObject
583+
* @modulecondition leaflet:L.Marker|L.CircleMarker|L.Circle|L.GeoJSON|L.Polyline|L.Polygon|GeoJSONObject
584+
* @modulecondition mapboxgl:mapboxgl.Point|mapboxgl.LngLatBounds|GeoJSONObject
585+
* @modulecondition maplibregl:maplibregl.Point|maplibregl.LngLatBounds|GeoJSONObject
586+
*/
587+
/**
588+
* @typedef {Object} ModuleBounds
589+
* @description 范围对象类型定义。<br>
590+
* @modulecondition openlayers:ol.extent|GeoJSONObject
591+
* @modulecondition leaflet:L.Bounds|L.LatLngBounds|GeoJSONObject
592+
* @modulecondition mapboxgl:mapboxgl.LngLatBounds|GeoJSONObject
593+
* @modulecondition maplibregl:maplibregl.LngLatBounds|GeoJSONObject
594+
*/
595+
596+
/**
597+
* @typedef {Object} ModulePolygon
598+
* @description 面对象类型定义。<br>
599+
* @modulecondition openlayers:ol.geom.Polygon|GeoJSONObject
600+
* @modulecondition leaflet:L.Polygon|L.GeoJSON|GeoJSONObject
601+
* @modulecondition mapboxgl:GeoJSONObject
602+
* @modulecondition maplibregl:GeoJSONObject
603+
*/
604+
605+
/**
606+
* @typedef {Object} ModulePolygonAndBounds
607+
* @description 范围和面对象类型定义。<br>
608+
* @modulecondition openlayers:ol.extent|ol.geom.Polygon|GeoJSONObject
609+
* @modulecondition leaflet:L.Bounds|L.LatLngBounds|L.Rectangle|L.Polygon|L.GeoJSON|GeoJSONObject
610+
* @modulecondition mapboxgl:mapboxgl.LngLatBounds|GeoJSONObject
611+
* @modulecondition maplibregl:maplibregl.LngLatBounds|GeoJSONObject
612+
*/
613+
614+
/**
615+
* @typedef {Object} ModulePolyline
616+
* @description 线对象类型定义。<br>
617+
* @modulecondition openlayers:ol.geom.LineString|GeoJSONObject
618+
* @modulecondition leaflet:L.Polyline|GeoJSONObject
619+
* @modulecondition mapboxgl:GeoJSONObject
620+
* @modulecondition maplibregl:GeoJSONObject
621+
*/

src/leaflet/core/CommontypesConversion.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import L from 'leaflet';
55
import './Base';
66
import { Bounds } from '@supermapgis/iclient-common/commontypes/Bounds';
7+
import { GeoJSON as GeoJSONFormat } from '@supermapgis/iclient-common/format/GeoJSON';
78

89
const isArray = function(obj) {
910
return Object.prototype.toString.call(obj) == '[object Array]';
@@ -22,15 +23,23 @@ export const CommontypesConversion = {
2223
* @returns {Bounds} SuperMap 的 bounds 对象。
2324
*/
2425
toSuperMapBounds(bounds) {
25-
if (bounds && ["FeatureCollection", "Feature"].indexOf(bounds.type) !== -1) {
26+
if (!bounds) {
27+
return new Bounds();
28+
}
29+
if (bounds instanceof Bounds) {
30+
return bounds;
31+
}
32+
if (GeoJSONFormat.getGeoJSONType(bounds)) {
2633
bounds = L.geoJSON(bounds).getBounds();
2734
}
2835
if (bounds instanceof L.LatLngBounds) {
36+
const southWest = bounds.getSouthWest();
37+
const northEast = bounds.getNorthEast();
2938
return new Bounds(
30-
bounds.getSouthWest().lng,
31-
bounds.getSouthWest().lat,
32-
bounds.getNorthEast().lng,
33-
bounds.getNorthEast().lat
39+
southWest.lng,
40+
southWest.lat,
41+
northEast.lng,
42+
northEast.lat
3443
);
3544
}
3645
if (bounds instanceof L.Bounds) {
@@ -41,10 +50,7 @@ export const CommontypesConversion = {
4150
bounds.max.y
4251
);
4352
}
44-
if (bounds instanceof Bounds) {
45-
return bounds;
46-
}
47-
if (isArray(bounds)) {
53+
if (isArray(bounds) && bounds.length >= 4) {
4854
return new Bounds(
4955
bounds[0],
5056
bounds[1],

src/leaflet/core/Util.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* @function toSuperMapGeometry
5858
* @category BaseTypes Util
5959
* @description 将 GeoJSON 对象转为 SuperMap 几何图形。
60-
* @param {GeoJSONObject} geometry - GeoJSON 对象。
60+
* @param {GeoJSONObject|ModuleGeometry} geometry - GeoJSON 对象。
6161
* @usage
6262
* ```
6363
* // 浏览器
@@ -79,8 +79,8 @@ export var toSuperMapGeometry = function(geometry) {
7979
}
8080
var result,
8181
format = new GeoJSONFormat();
82-
if (['FeatureCollection', 'Feature', 'Geometry'].indexOf(geometry.type) != -1) {
83-
result = format.read(geometry, geometry.type);
82+
if (GeoJSONFormat.isGeoJSON(geometry)) {
83+
result = format.read(geometry, GeoJSONFormat.getGeoJSONType(geometry));
8484
} else if (typeof geometry.toGeoJSON === 'function') {
8585
var geojson = geometry.toGeoJSON();
8686
result = geojson ? format.read(geojson, geojson.type) : geometry;

src/openlayers/core/Util.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import Feature from 'ol/Feature';
1616
import Projection from 'ol/proj/Projection';
1717
import { get } from 'ol/proj';
18+
import GeoJSON from 'ol/format/GeoJSON';
1819

1920
/**
2021
* @name Util
@@ -67,16 +68,26 @@
6768

6869
/**
6970
* @function Util.toSuperMapGeometry
70-
* @description 将 GeoJSON 对象转为 SuperMap 几何图形
71-
* @param {GeoJSONObject} geoJSON - GeoJSON 对象。
71+
* @description 将 GeoJSON 对象 或 OpenaLayers 几何对象要素对象转为 SuperMap 几何对象
72+
* @param {GeoJSONObject|ModuleGeometry} obj - GeoJSON 对象 或 OpenaLayers 几何对象要素对象
7273
* @private
7374
*/
74-
toSuperMapGeometry(geoJSON) {
75-
if (!geoJSON || !geoJSON.type) {
75+
toSuperMapGeometry(obj) {
76+
if (!obj) {
7677
return null;
7778
}
78-
const result = new GeoJSONFormat().read(geoJSON, 'FeatureCollection');
79-
return result && result[0].geometry;
79+
let result = obj;
80+
if (obj instanceof Geometry) {
81+
result = new GeoJSON().writeGeometryObject(obj);
82+
}
83+
if (obj instanceof Feature) {
84+
result = new GeoJSON().writeFeatureObject(obj);
85+
}
86+
if (GeoJSONFormat.isGeoJSON(result)) {
87+
const newFeatures = new GeoJSONFormat().read(result, 'FeatureCollection');
88+
return newFeatures && newFeatures[0].geometry;
89+
}
90+
return null;
8091
},
8192

8293
/**

src/openlayers/mapping/ImageSuperMapRest.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { ServerGeometry } from '@supermapgis/iclient-common/iServer/ServerGeomet
88
import { Util } from '../core/Util';
99
import ImageSource from 'ol/source/Image';
1010
import ImageWrapper, { decode } from 'ol/Image';
11-
import Geometry from 'ol/geom/Geometry';
12-
import GeoJSON from 'ol/format/GeoJSON';
1311
import { containsExtent, getCenter, getHeight, getWidth, getForViewAndSize } from 'ol/extent';
1412

1513
/**
@@ -109,8 +107,8 @@ export class ImageSuperMapRest extends ImageSource {
109107
if (options.prjCoordSys) {
110108
params['prjCoordSys'] = JSON.stringify(options.prjCoordSys);
111109
}
112-
if (options.clipRegionEnabled && options.clipRegion instanceof Geometry) {
113-
options.clipRegion = Util.toSuperMapGeometry(new GeoJSON().writeGeometryObject(options.clipRegion));
110+
if (options.clipRegionEnabled) {
111+
options.clipRegion = Util.toSuperMapGeometry(options.clipRegion);
114112
options.clipRegion = CommonUtil.toJSON(ServerGeometry.fromGeometry(options.clipRegion));
115113
params['clipRegionEnabled'] = options.clipRegionEnabled;
116114
params['clipRegion'] = JSON.stringify(options.clipRegion);

src/openlayers/mapping/TileSuperMapRest.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { ServerGeometry } from '@supermapgis/iclient-common/iServer/ServerGeomet
88
import { Util } from '../core/Util';
99
import TileImage from 'ol/source/TileImage';
1010
import Geometry from 'ol/geom/Geometry';
11-
import GeoJSON from 'ol/format/GeoJSON';
1211
import * as olSize from 'ol/size';
1312
import * as olTilegrid from 'ol/tilegrid';
1413
import TileGrid from 'ol/tilegrid/TileGrid';
@@ -118,7 +117,7 @@ export class TileSuperMapRest extends TileImage {
118117

119118
if (options.clipRegion instanceof Geometry) {
120119
options.clipRegionEnabled = true;
121-
options.clipRegion = Util.toSuperMapGeometry(new GeoJSON().writeGeometryObject(options.clipRegion));
120+
options.clipRegion = Util.toSuperMapGeometry(options.clipRegion);
122121
options.clipRegion = CommonUtil.toJSON(ServerGeometry.fromGeometry(options.clipRegion));
123122
params['clipRegionEnabled'] = options.clipRegionEnabled;
124123
params['clipRegion'] = JSON.stringify(options.clipRegion);

src/openlayers/services/FeatureService.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { EditFeaturesService } from '@supermapgis/iclient-common/iServer/EditFea
77
import { FeatureService as CommonFeatureService } from '@supermapgis/iclient-common/iServer/FeatureService';
88
import { Util } from '../core/Util';
99
import { ServiceBase } from './ServiceBase';
10-
import GeoJSON from 'ol/format/GeoJSON';
11-
import Geometry from 'ol/geom/Geometry';
1210
import Polygon from 'ol/geom/Polygon';
1311

1412
/**
@@ -200,8 +198,8 @@ export class FeatureService extends ServiceBase {
200198
if (params.bounds) {
201199
params.bounds = Util.toSuperMapBounds(params.bounds);
202200
}
203-
if (params.geometry && params.geometry instanceof Geometry) {
204-
params.geometry = Util.toSuperMapGeometry(JSON.parse(new GeoJSON().writeGeometry(params.geometry)));
201+
if (params.geometry) {
202+
params.geometry = Util.toSuperMapGeometry(params.geometry);
205203
}
206204
if (params.editType) {
207205
params.editType = params.editType.toLowerCase();
@@ -229,10 +227,7 @@ export class FeatureService extends ServiceBase {
229227
return geoFeature.toServerFeature({geometryFunction:(geometry)=>{
230228
if(Array.isArray(geometry) && geometry.length === 4){
231229
const polygon = new Polygon([[[geometry[0], geometry[1]], [geometry[2], geometry[1]], [geometry[2], geometry[3]], [geometry[0], geometry[3]]]]);
232-
return Util.toSuperMapGeometry(new GeoJSON().writeGeometryObject(polygon));
233-
}
234-
if(geometry.getExtent){
235-
return Util.toSuperMapGeometry(new GeoJSON().writeGeometryObject(geometry));
230+
return Util.toSuperMapGeometry(polygon);
236231
}
237232
return Util.toSuperMapGeometry(geometry);
238233
}})
@@ -250,7 +245,7 @@ export class FeatureService extends ServiceBase {
250245
if (geoFeature.getId()) {
251246
feature.id = geoFeature.getId();
252247
}
253-
feature.geometry = Util.toSuperMapGeometry(new GeoJSON().writeFeatureObject(geoFeature));
248+
feature.geometry = Util.toSuperMapGeometry(geoFeature);
254249
return feature;
255250
}
256251

src/openlayers/services/MeasureService.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import {Util} from '../core/Util';
55
import {ServiceBase} from './ServiceBase';
66
import { MeasureService as CommonMeasureService } from '@supermapgis/iclient-common/iServer/MeasureService';
7-
import GeoJSON from 'ol/format/GeoJSON';
87

98
/**
109
* @class MeasureService
@@ -70,7 +69,7 @@ export class MeasureService extends ServiceBase {
7069

7170
_processParam(params) {
7271
if (params && params.geometry) {
73-
params.geometry = Util.toSuperMapGeometry(JSON.parse((new GeoJSON()).writeGeometry(params.geometry)));
72+
params.geometry = Util.toSuperMapGeometry(params.geometry);
7473
}
7574
return params;
7675
}

src/openlayers/services/QueryService.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {Util} from '../core/Util';
77
import {ServiceBase} from './ServiceBase';
88
import { QueryService as CommonQueryService } from '@supermapgis/iclient-common/iServer/QueryService';
99
import Point from 'ol/geom/Point';
10-
import GeoJSON from 'ol/format/GeoJSON';
1110

1211
/**
1312
* @class QueryService
@@ -108,7 +107,7 @@ export class QueryService extends ServiceBase {
108107
if (params.geometry instanceof Point) {
109108
params.geometry = new GeometryPoint(params.geometry.getCoordinates()[0], params.geometry.getCoordinates()[1]);
110109
} else {
111-
params.geometry = Util.toSuperMapGeometry(JSON.parse((new GeoJSON()).writeGeometry(params.geometry)));
110+
params.geometry = Util.toSuperMapGeometry(params.geometry);
112111
}
113112
}
114113
return params;

0 commit comments

Comments
 (0)