Layer specific geometry drawing

Hi,

I want to draw a polygon, line or another geometry type on a selected layer only. I can draw geometry globally but in detailed scenario I want to create a temp layer and draw a geometry on it, if user wants to save this layer then I want to save it to database. Another usage scenario is when I get WMS layer from server and show it on cesium I also want to draw only on this WMS layer.

I can draw geometries, I have imageryLayersCollection so can I do these scenarios with Cesium and how?

Thanks

Welcome to the Cesium community!

Can you describe what you mean by draw only on a selected layer? You can create geometry with CesiumJS but it’s not tied to any particular layer. If you want to allow your user to draw only in a specified region or rectangle, you can do this by detecting if the mouse click is inside your defined region before drawing.

If you haven’t already, this Drawing on Terrain Sandcastle example may be helpful: https://sandcastle.cesium.com/index.html?src=Drawing%20on%20Terrain.html.

Hi Omar,

I have WMS layers and I want to draw on only one of them. I have some workarounds for this purpose. I have seen Cesium has CustomDataSource so I thought that maybe I can store a <key,value> map with layer as a key and its CustomDataSource to store new drawn entities as value. Then I can achieve that when user set visibility of layers I can check that <key,value> map and I can iterate over its entities then I can set visibility of entities. If user want to save those entities to the layer then I can iterate again and send request to the GeoServer with GML, GeoJSON or something else.

This is what I want to do and here is my solution for it. Maybe you have suggestion for this purpose.

Thanks

I think maybe you have two requirements:

  • Create a collection of shapes that can be managed as a unit, and
  • Save that collection somewhere so you can reload it later

For the first, CustomDataSource is a good solution. You just create one and add it to viewer.dataSources, then you can draw individual geometries by creating an Entity in mySource.entities. The whole CustomDataSource can be toggled using mySource.show.

For the second, look at the exportKml global function. Pass this your mySource.entities collection and it will spit out standardized KML that you can save off in your data store of choice, then reload later using a KmlDataSource. Actually, now that I think of it, I’m pretty sure you could make a KmlDataSource and add your own geometry to its entities collection, if you really wanted.