Creating an ESRI Geometry from GML 2.0 using the Core & CoreHost API

1603
3
03-12-2020 03:00 AM
LarsDomesjö
New Contributor III

Hello,

I in the process of writing a utility which creates ArcGIS Features in a geodatabase based on information coming in via an XML.

This XML contains geometries in GML 2.0-format which I wish to convert into Shapes on a Feature.

I looked around within the ArcGIS Pro Core API and couldn't find built-in functionality to import shapes from GML so i started using GDAL to convert the geometries from GML 2.0 to ESRI Shapefiles, as the geometryengine does have a function to create geometries from ESRI Shapefiles.

I am currently using the following C#-code to create the ESRI Shapefile using GDAL:

using (var g = OSGeo.OGR.Geometry.CreateFromGML(gmlElement.ToString()))
using (var esriDriver = Ogr.GetDriverByName("ESRI Shapefile"))
using (var esriDataSource = esriDriver.CreateDataSource(tempFile, new string[] {}))
using (var esriLayer = esriDataSource.CreateLayer("baseLayer", spatialReference, g.GetGeometryType(), new string[] {}))
using (var esriLayerDefinition = esriLayer.GetLayerDefn())
using (var feature = new Feature(esriLayerDefinition))
{
feature.SetGeometry(g);
esriLayer.CreateField(new FieldDefn("id", FieldType.OFTInteger), 0);

feature.SetField("id", 123);
var i = esriLayer.CreateFeature(feature);

}

var bytes = File.ReadAllBytes(tempFile);

I am then using the following code to import into a ESRI Geometry from the ESRI Shapefile byte-array created using GDAL:

var geometry = GeometryEngine.Instance.ImportFromEsriShape(EsriShapeImportFlags.esriShapeImportNoSwap, bytes, CreateSpatialReference(3006));

the ArcGIS Pro SDK does not seem to like this however as it seems to be unable to find any geometries within the provided ESRI Shapefile:

ArcGIS.Core.Geometry.GeometryObjectException: 'GeometryObjectException: A null geometry does not correspond to any Esri geometry type.'

Do you have any hints for me on what I might be doing wrong? Or know of a better way to go from a shape in GML 2.0 format to an ESRI Geometry using the Core & CoreHost APIs?

0 Kudos
3 Replies
by Anonymous User
Not applicable

Hi Lars Domesjö‌,

The error seem to be happening at this line => 

 using (var esriLayer = esriDataSource.CreateLayer("baseLayer", spatialReference, g.GetGeometryType(), new string[] {}))

g look like null.

Apart from GeometryEngine line of code, you seem to be using the other external libraries outside of ArcGIS runtime and ArcGIS pro sdk. 

Additionally I assume tempFile is the shape file from your code, you can test whether you successfully generated or not by importing into ArcGIS pro or ArcMap and visualize it.

Best Regards,

0 Kudos
NarelleChedzey
Esri Contributor

Hi Lars, 

As you found, GML 2.0 format is not currently supported in the Pro API.    I also don't have knowledge of the GDAL libraries you are using to create a shapefile, but I would check the following.

Firstly a shapefile is composed of at least 3 files; a .shp, a .shx and a .dbf file.  You need to make sure that all three of these files are created.   

Secondly I would attempt to load the output shapefile into ArcMap or ArcGIS Pro to ensure that you have geometries, and that the geometries match what you expect from your source data.

After this you will need to read the shapefile. Rather than using the ImportFromEsriShape function (which does not work on an entire shapefile but on a feature by feature basis), you should use the geodatabase API which is available in ArcGIS.Core.Data to connect to the shapefile.  Here is a snippet that should help

https://github.com/Esri/arcgis-pro-sdk/wiki/ProSnippets-Geodatabase#opening-a-featureclass-from-a-sh... 

Once you have the shapefile feature class open you should be able to use a search cursor to obtain all the rows and iterate through to obtain the geometries. 

I hope this helps.  Let me know if you have additional questions. 

Regards

Narelle

LarsDomesjö
New Contributor III

Thanks Narelle!  I went another route and went from GML to WKB (via an external library) and then from WKB to ESRI Geometries in the end.

Will try to try this at some point as well though.

0 Kudos