This project has moved. For the latest updates, please go here.

adding multiplegepmetry to a placemark

Apr 9, 2013 at 12:41 PM
Edited Apr 9, 2013 at 12:42 PM
hi
i have a problem with the "MultipleGeometry" class.
I want to add to a placemark 2 geometry elements (for example 2 polygon), so i wrote...
.
.
.
MultipleGeometry mGeometry = new MultipleGeometry();
mGeometry.AddGeometry(polygon1);
mGeometry.AddGeometry(polygon2);
.
.
placeMark.Geometry = mGeometry;

This piece of code doesn't work :/
I try this too and it works:
.
MultipleGeometry mGeometry = new MultipleGeometry();
mGeometry.AddGeometry(polygon1);
.
i get this in my kml file

<MultiGeometry>
    <Polygon>
      <extrude>true</extrude>
      <tessellate>true</tessellate>
      <altitudeMode>clampToGround</altitudeMode>
      <outerBoundaryIs>
        <LinearRing>
          <coordinates>52.62,-0.12,0
52.4,1.45,0
50.53,0.72,0
50.75,-0.78,0
52.62,-0.12,0</coordinates>
        </LinearRing>
      </outerBoundaryIs>
    </Polygon>
  </MultiGeometry>

instead with the first piece of code i get no multipleGeometry entry in the kml file.

Do i write something wrong?

Thanks a lot
Costanzo
Apr 10, 2013 at 10:22 AM
Trying to solve the problem, i found this problem with the MultipleGeometry



I try to add only one polygon (mGeometry.AddGeometry(polygon1);) and then i serialize the file. The output kml was written fine, but when i tried to add a second polygon i got the following exception:

<ExceptionElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ngeo.eo.esa.int/schema/webserver">
<exceptionCode>Error</exceptionCode>
<exceptionMessage>Cannot add child element to this instance because it belongs to another instance.</exceptionMessage>
<exceptionStackTrace>
at SharpKml.Dom.Element.AddChild[Geometry] (SharpKml.Dom.Geometry child) [0x00000] in <filename unknown>:0 at SharpKml.Dom.MultipleGeometry.AddGeometry (SharpKml.Dom.Geometry geometry) [0x00000] in <filename unknown>:0 at Connector.Catalogue.KMLAssembly.KMLGeometry (System.Xml.XmlNode node, System.Xml.XmlNamespaceManager xnsm) [0x002e5] in /Users/cdangelis1/Repository/live/sugar.portal.modules.ngeoconnector.library/NgEO/Connector/Catalogue/KMLAssembly.cs:707 at Connector.Catalogue.KMLAssembly.QueryAtomToKML (Portal.OpenSearchRequest request,OpenSearch.OpenSearchUrl url) [0x00e6f] in /Users/cdangelis1/Repository/live/sugar.portal.modules.ngeoconnector.library/NgEO/Connector/Catalogue/KMLAssembly.cs:525 at Portal.OpenSearchEngine.Query (Portal.OpenSearchRequest request) [0x00077] in /Users/cdangelis1/Repository/live/sugar.portal.core.library/Portal/Search.cs:125 at WebServer.CatalogueSearch.ProductSearch.search () [0x0009e] in /Users/cdangelis1/Repository/live/ngeo.webserver.library/NgEO/WebServer/CatalogueSearch/ProductSearchService.cs:92 at WebServer.CatalogueSearch.ProductSearchService.Execute (WebServer.CatalogueSearch.ProductSearch productSearch) [0x00000] in /Users/cdangelis1/Repository/live/ngeo.webserver.library/NgEO/WebServer/CatalogueSearch/ProductSearchService.cs:116
</exceptionStackTrace>
</ExceptionElement>
Coordinator
Apr 11, 2013 at 6:26 AM
I'm not sure why it's not working to be honest. The exception you are getting is probably because you are adding the same polygon instance to the MulipleGeometry - you can use the Clone extension method from the SharpKml.Engine namespace to help you create a copy.

Can you show me any code that reproduces the original problem of the MultipleGeometry not serializing properly so I can try and diagnose it further?

Thanks,
Sam
Apr 11, 2013 at 8:02 AM
Thanks a lot samcragg
I get the error because i use the same polygon object to test my procedure. I didn't think to add a different polygon to the multiplegeometry, i believed i could add the same object twice or more. Now, trying with a second different polygon from the first one, i get the right kml output. So the original problem came from my coding (i tried to add the same polygon object so i got an error, but i put this part of code inside a try cacth statement, so the multiplegeometry node was not added to the kml).
Thanks a lot :)