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.

hi, layer.SaveTo have SavingOptions but have no ConversionOptions , we can not set output accuracy , Is there a way out?

Hi, @lsl

Please consider the below code

        var inputOptions = new FileGdbOptions(){};
        var outputOptions = new GeoJsonOptions() { XYPrecisionModel = PrecisionModel.Rounding(2) };
        using (var layer = Drivers.FileGdb.OpenDataset(gdbPath, inputOptions))
        {
            layer.OpenLayerAt(1).SaveTo(jsonPath, Drivers.GeoJson, new SavingOptions()
            {
                DriverOptions = outputOptions
            });
        }

Thanks

thanks , it work for me . But I found a new question. The format of exported geojson is different from that of GDAL exported geojson , for example: no crs , no id in feature

I found too that CRS is missing in exported GeoJSON. The GISNET-1286 ticket has been created to fix this issue.

I can’t reproduce the missed Ids. Could you provide source file (or its part)?

Thanks.

{ “type”: “Feature”, “id”: 1, “properties”: { “XZQ”: “1111111111”}}

"id": 1,
id is missing in exported GeoJSON , id is just fid in shp

We keep all attributes(including ID) in the property list.

{ “type”: “Feature”, “properties”: { “id”: 1, “XZQ”: “1111111111”}}

Although If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name “id”. Thank you for pointing that out.

I have created the GISNET-1287 ticket. We will add the id_field option.

Hello, @lsl

Aspose.GIS 22.1 for .NET has been released. This release includes the fix for GISNET-1287.

Best regards.