Skip to content

Commit

Permalink
prevent duplicates option in GeoJsonDataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
jjspace committed Feb 3, 2025
1 parent 3fb9c54 commit f44c30b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,9 @@
createdNewDataSource = true;
}
const resource = createCollectionResource(baseUrl, collectionId, options);
loadedGeoJson = await loadedGeoJson.process(resource);
loadedGeoJson = await loadedGeoJson.process(resource, {
preventDuplicates: true,
});

if (createdNewDataSource) {
viewer.dataSources.add(loadedGeoJson);
Expand Down
43 changes: 40 additions & 3 deletions packages/engine/Source/DataSources/GeoJsonDataSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,16 @@ function defaultDescribeProperty(properties, nameProperty) {
//GeoJSON specifies only the Feature object has a usable id property
//But since "multi" geometries create multiple entity,
//we can't use it for them either.
function createObject(geoJson, entityCollection, describe) {
function createObject(
geoJson,
entityCollection,
describe,
preventDuplicates = false,
) {
let id = geoJson.id;
if (!defined(id) || geoJson.type !== "Feature") {
id = createGuid();
} else {
} else if (!preventDuplicates) {
let i = 2;
let finalId = id;
while (defined(entityCollection.getById(finalId))) {
Expand All @@ -124,6 +129,10 @@ function createObject(geoJson, entityCollection, describe) {
id = finalId;
}

if (entityCollection.getById(id) && preventDuplicates) {
return;
}

const entity = entityCollection.getOrCreateEntity(id);
const properties = geoJson.properties;
if (defined(properties)) {
Expand Down Expand Up @@ -215,7 +224,12 @@ const geometryTypes = {
function processFeature(dataSource, feature, notUsed, crsFunction, options) {
if (feature.geometry === null) {
//Null geometry is allowed, so just create an empty entity instance for it.
createObject(feature, dataSource._entityCollection, options.describe);
createObject(
feature,
dataSource._entityCollection,
options.describe,
options.preventDuplicates,
);
return;
}

Expand Down Expand Up @@ -313,7 +327,11 @@ function createPoint(dataSource, geoJson, crsFunction, coordinates, options) {
geoJson,
dataSource._entityCollection,
options.describe,
options.preventDuplicates,
);
if (!defined(entity)) {
return;
}
entity.billboard = billboard;
entity.position = new ConstantPositionProperty(crsFunction(coordinates));

Expand Down Expand Up @@ -385,7 +403,11 @@ function createLineString(
geoJson,
dataSource._entityCollection,
options.describe,
options.preventDuplicates,
);
if (!defined(entity)) {
return;
}
const polylineGraphics = new PolylineGraphics();
entity.polyline = polylineGraphics;

Expand Down Expand Up @@ -512,7 +534,11 @@ function createPolygon(dataSource, geoJson, crsFunction, coordinates, options) {
geoJson,
dataSource._entityCollection,
options.describe,
options.preventDuplicates,
);
if (!defined(entity)) {
return;
}
entity.polygon = polygon;
}

Expand Down Expand Up @@ -564,6 +590,7 @@ function processTopology(dataSource, geoJson, geometry, crsFunction, options) {
* @property {Color} [fill=GeoJsonDataSource.fill] The default color for polygon interiors.
* @property {boolean} [clampToGround=GeoJsonDataSource.clampToGround] true if we want the geometry features (polygons or linestrings) clamped to the ground.
* @property {Credit|string} [credit] A credit for the data source, which is displayed on the canvas.
* @property {boolean} [preventDuplicates=false] Prevent createing duplicate entities based on matching ids
*/

/**
Expand Down Expand Up @@ -906,6 +933,15 @@ GeoJsonDataSource.prototype.process = function (data, options) {
return preload(this, data, options, false);
};

/**
* @private
*
* @param {GeoJsonDataSource} that
* @param {Resource|string|object} data
* @param {GeoJsonDataSource.LoadOptions} options
* @param {boolean} clear
* @returns
*/
function preload(that, data, options, clear) {
//>>includeStart('debug', pragmas.debug);
if (!defined(data)) {
Expand Down Expand Up @@ -956,6 +992,7 @@ function preload(that, data, options, clear) {
defaultValue(options.fill, defaultFill),
),
clampToGround: defaultValue(options.clampToGround, defaultClampToGround),
preventDuplicates: options.preventDuplicates ?? false,
};

return Promise.resolve(promise)
Expand Down

0 comments on commit f44c30b

Please sign in to comment.