Someone knows what im doing wrong with the clipping(i see the cesium tutorial in cesium page , but for sure im missing something) ? Or some other way to clip a geojson file. Thanks
import {
Ion,
Viewer,
Terrain,
createGooglePhotorealistic3DTileset,
GeoJsonDataSource,
IonResource,
ClippingPolygonCollection,
ClippingPolygon,
Cartesian3,
Math as CesiumMath,
Color,
HeadingPitchRange,
SceneMode
} from “cesium”;
import “cesium/Widgets/widgets.css”;
import “./css/main.css”;
// — CONFIGURAÇÕES —
Ion.defaultAccessToken = xxxx
// ID do GeoJSON de Portugal no Cesium Ion
const FOOTPRINT_ID = xxxxx;
async function startPortugalClippedView() {
const viewer = new Viewer(“cesiumContainer”, {
terrain: Terrain.fromWorldTerrain(),
sceneMode: SceneMode.SCENE3D,
timeline: false,
animation: false,
baseLayerPicker: true,
geocoder: true,
shouldAnimate: true,
});
// Mostrar o globo
viewer.scene.globe.show = true;
viewer.scene.skyAtmosphere.show = true;
// — Carregar Photorealistic 3D Tiles —
let googleTileset;
try {
googleTileset = await createGooglePhotorealistic3DTileset({
onlyUsingWithGoogleGeocoder: true
});
viewer.scene.primitives.add(googleTileset);
} catch (error) {
console.log(Erro ao carregar Photorealistic 3D Tiles: ${error});
}
// — Carregar footprint de Portugal —
let footprint;
try {
const resource = await IonResource.fromAssetId(FOOTPRINT_ID);
const dataSource = await GeoJsonDataSource.load(resource, { clampToGround: true });
viewer.dataSources.add(dataSource);
footprint = dataSource.entities.values.find(entity => Cesium.defined(entity.polygon));
footprint.polygon.outline = true;
footprint.polygon.material = Color.YELLOW.withAlpha(0.3);
// Zoom inicial para Portugal
const cameraOffset = new HeadingPitchRange(
CesiumMath.toRadians(0),
CesiumMath.toRadians(-30),
200000.0
);
viewer.zoomTo(footprint, cameraOffset);
viewer.homeButton.viewModel.command.beforeExecute.addEventListener((e) => {
e.cancel = true;
viewer.zoomTo(footprint, cameraOffset);
});
} catch (error) {
console.log(Erro ao carregar GeoJSON: ${error});
}
// — Criar Clipping Polygon para mostrar apenas Portugal —
if (footprint && googleTileset) {
const positions = footprint.polygon.hierarchy.getValue().positions;
const clippingPolygons = new ClippingPolygonCollection({
polygons: [new ClippingPolygon({ positions })],
edgeColor: Color.YELLOW,
edgeWidth: 2.0
});
// Inverso para mostrar apenas dentro de Portugal
clippingPolygons.inverse = true;
// Aplicar ao tileset
googleTileset.clippingPolygons = clippingPolygons;
}
}
startPortugalClippedView();
