Free Support Forum - aspose.com

How convert one layer in gdb or one feature to geojson

image we have a gdb which contain 3 layers(a,b,c), we only want to convert to geojson for b or one feature

using (var dataset = Dataset.Open(dataDir + "ThreeLayers.gdb", Drivers.FileGdb))
{
    Console.WriteLine("FileGDB has {0} layers", dataset.LayersCount);
    for (int i = 0; i < dataset.LayersCount; ++i)
    {
        Console.WriteLine("Layer {0} name: {1}", i, dataset.GetLayerName(i));

        using (var layer = dataset.OpenLayerAt(i))
        {
//how convert layer to geojson?   we pass geojson path  or  method return string
            Console.WriteLine("Layer has {0} features", layer.Count);
            foreach (var feature in layer)
            {//how convert feature to geojson?
                Console.WriteLine(feature.Geometry.AsText());
            }
        }
        Console.WriteLine("");
    }
}

Hello, @lsl,

Thank you for your interest in the Aspose.GIS product.

Please, consider the following code to extract an one layer from Gdb:

        var gdbPath = "ThreeLayers.gdb";
        var jsonPath = "all_from_layer01.json";
        using (var layer = Dataset.Open(gdbPath, Drivers.FileGdb))
        {
            layer.OpenLayerAt(1).SaveTo(jsonPath, Drivers.GeoJson);
        }

And this code extracts the features in even indexes.

        var gdbPath = "ThreeLayers.gdb";
        var jsonPath = "even_from_layer01.json";

        using (var inputGdb = Dataset.Open(gdbPath, Drivers.FileGdb))
        using (var inputLayer = inputGdb.OpenLayerAt(1))
        {
            using (VectorLayer outputLayer = VectorLayer.Create(jsonPath, Drivers.GeoJson))
            {
                outputLayer.CopyAttributes(inputLayer);
                
                for (int i = 0; i < inputLayer.Count; i++)
                {
                    // only even
                    if (i % 2 == 0)
                    {
                        Feature inputFeature = inputLayer[i];
                        Feature outputFeature = outputLayer.ConstructFeature();
                        outputFeature.Geometry = inputFeature.Geometry;
                        outputFeature.CopyValues(inputFeature);
                        outputLayer.Add(outputFeature);
                    }
                }
            }
        }

Best regards.

yes, I get it
but how about convert one feature to geojson string?

GISNET-346 ---- Status : Open
GISNET-348 ---- Status : Closed
GISNET-350 ---- Status : Open
GISNET-351 ---- Status : Open
thnks for your hard work, above feature is very helpful ,Is there any progress

Hi, @lsl,

this example convert each gdb-feature in the separated geo-json layers (in memory) and prints as strings in console. I hope its help you.

        // open a gdb layer at '1' index for reading.
        using (var inputGdb = Dataset.Open(gdbPath, Drivers.FileGdb))
        using (var inputLayer = inputGdb.OpenLayerAt(1))
        {
            // print each features as geo-json
            foreach (var inputFeature in inputLayer)
            {
                using (var stream = new MemoryStream())
                {
                    // create a new geo-json layer in memory stream.
                    var jsonPath = AbstractPath.FromStream(stream);
                    using (VectorLayer outputLayer = VectorLayer.Create(jsonPath, Drivers.GeoJson))
                    {
                        // take gdb attributes schema and write this schema to new geo-json layer.
                        outputLayer.CopyAttributes(inputLayer);

                        // first we need to create a new feature with attributes.
                        Feature outputFeature = outputLayer.ConstructFeature();

                        // then we take a geometry from gdb and set to geo-json.
                        // here we can to add logic that modify output geometry (srs transformation, modification etc).
                        outputFeature.Geometry = inputFeature.Geometry;

                        // then we copies all values of attributes from another feature.
                        // here we can to add logic that modify output attributes values.
                        outputFeature.CopyValues(inputFeature);

                        // adds a new feature to the layer.
                        outputLayer.Add(outputFeature);
                    }

                    // convert stream to string
                    stream.Seek(0, SeekOrigin.Begin);
                    StreamReader reader = new StreamReader(stream);
                    string geoJson = reader.ReadToEnd();
                    Console.WriteLine(geoJson);
                }
            }
        }

And thank you that confirmed the relevance of open tasks.

In the previous year, we implemented primarily tasks that were recommended by business analytics. And we could not pay attention to the tasks associated with updating the layer data. Now we are making major efforts to implement the tasks from the forum.

I researched the discussion from the ‘How modify Gdb’ topic. And it is important to be able to update the large data sets. We will try to add this new functionality.

Thanks.