Aspose.GIS visualize geometry (miniature and full size , image / svg)

Hi,
I have an application where I use EntityFramework (MSSQL database ) to show the attribute table of data with a geometry column. this geometry column contains (multi) polygons .
I want to show a “miniature” image / svg / png of the polygon / shape in a row or detail view in my application.
I have not been able to do this with Aspose.Gis.

What I was expecting was that I can use Aspose.Gis to instanciate a Aspose.Gis.Feature.Geometry and then just have to say … Feature.ToSVG or Feature.ToPNG to get the image or svg data. base64 data is also ok and usable in a tabular form.

image.png (3.9 KB)

how can I do this with Aspose.Gis
Hope you can guide
Regards.
MC

@mcanavar,

We will evaluate your requirements and get back to you soon.

@mcanavar,

Please notice, we have logged an investigation ticket with an id “GISNET-1326” for your task/issue. We will evaluate and look into the details of your requirements.

Once we have an update on it, we will let you know here.

1 Like

This is actually a very usefull functional addition to generate SVG or PNG of the geometry with a given width and height.

I’m not asking for a timeline , but wanted to bring this issue to your attention again .

Hi, @mcanavar

As a workaround, you need to create a new layer (in memory using some driver) and put the geometry there. Next step, you can draw this layer on a map.

We plan to create an extension to hide this implementation. if you still have questions, let me know

Hi, @mcanavar

The Ticket (GISNET-1326) have been completed in Aspose.GIS for .NET 22.05 . Please feel free to contact us if you have any other issues or difficulty using the product.

1 Like

Hi @Evgeniy.Timofeev,

Thank you for implementing this request.

I have been able to stream and export to file using different renderers with one line. like this:

geometry.AsImage(filenamepath, new Measurement(200, Unit.Pixels), new Measurement(200, Unit.Pixels), Renderers.Png);

and

var imageStream = geometry.AsImage(new Measurement(200, Unit.Pixels), new Measurement(200, Unit.Pixels), Renderers.Png);

This is great!

Only issue I’m now having is that I’m not able to determine the color of the renderd shape, how can I set the line and fill color of the shape . I’m working with Crop fields and each color represent the crop on that field .

One example of a png file renderd is attached.13608.png (1.9 KB)

1 Like

@mcanavar,

We will check and get back to you soon.

1 Like

Hi, @mcanavar
Please consider code below to determine the color of the rendered shapes, lines, points

        //use to write in stream
        var imageStream = new MemoryStream();
        AbstractPath path = AbstractPath.FromStream(imageStream);
        //use to write in file
        //AbstractPath path = AbstractPath.FromLocalPath(outputPath);

        // styles 
        var lineSymbolizer = new SimpleLine { Width = 1.5, Color = Color.DarkSlateBlue };
        var polygonSymbolizer = new SimpleFill { FillColor = Color.Azure, StrokeColor = Color.Brown };
        var pointSymbolizer = new SimpleMarker { FillColor = Color.Aqua, StrokeColor = Color.Red };
        var symbolizer = new MixedGeometrySymbolizer();
        symbolizer.LineSymbolizer = lineSymbolizer;
        symbolizer.PointSymbolizer = pointSymbolizer;
        symbolizer.PolygonSymbolizer = polygonSymbolizer;

        // render
        geometry.AsImage(this, path, Measurement.Pixels(200), Measurement.Pixels(200), Renderers.Png, symbolizer);

All feature to render: Symbology|Documentation (aspose.com)

Thanks

1 Like

Hi @Evgeniy.Timofeev,

This is what I was looking for, thank you for the example .
Apologies that I overlooked the thirt override of the AsImage function with the symbolizer property.

Kind Regards,
Mehmet

PS: Perhaps the property symbolizer of the AsImage function should be optional for or all three of the override function . .

Hi, @mcanavar

The symbolizer property has been added in all methods. The Ticket ( GISNET-1335) has been completed in Aspose.GIS for .NET 22.06 . Please feel free to contact us if you have any other issues or difficulty using the product.

1 Like

Hi,

I want to visualize as set of geometries in a collection and want to give each item in the collection their own color for border and fill and render this to memory stream. / png.

something like this ?

using System;
using Aspose.Gis;
using Aspose.Gis.Geometries;
using Aspose.Gis.Rendering;

namespace GeometryCollectionVisualization
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new map instance
            var map = new Map();
            var layer = map.AddLayer();

            // Create a geometry collection with different geometries
            var geometryCollection = new GeometryCollection
            {
                new Point(10, 20),
                new LineString(new[] {new Point(30, 40), new Point(50, 60)}),
                new Polygon(new[]
                {
                    new[]
                    {
                        new Point(70, 80),
                        new Point(90, 100),
                        new Point(110, 120),
                        new Point(70, 80)
                    }
                })
            };

            // Define colors for each geometry
            var colors = new[] {Color.Red, Color.Green, Color.Blue};

            // Visualize each geometry with its respective color
            for (int i = 0; i < geometryCollection.Count; i++)
            {
                var style = new FeatureStyle
                {
                    Fill = new SolidFill(colors[i])
                };
                
                // Create a feature and add it to the layer
                var feature = layer.AddFeature(geometryCollection[i]);
                feature.GeometryStyle = style;
            }

            // Render the map
            var options = new MapImageOptions
            {
                Format = MapImageFormat.Png
            };

            map.Render("output.png", options);
        }
    }
}

can this be done ?

regards,

Mehmet

Hi, @mcanavar

Geometry styles are used for formats with inline styling. It doesn’t work for rendering.

To solve your problem, a solution using FeatureBasedConfiguration is suitable. See an example at this link Map Rendering to Image SVG, PNG, JPG using GIS C# Library|Documentation (aspose.com). You can change the color, and other style options depending on the content.

Best regards

Hi @Evgeniy.Timofeev,

Thank you for your suggestion, but I did not succeed with this.
I only use the abstract path and in memory rendering , so no file paths or files available .

I have a collection of IGeometry objects that I want to display and want to give each of the objects a color based on some value in Objects attributes (like the example for FeatureBasedConfiguration ).

Background layer to openstreetmap would be nice , but a blank background is also fine.

I keep getting errors like : the layer does not support read access or combine is not supported .

It seemed so simple to add features to a map with its own styling or a layer with features that can make use of a style sheet like SLD , but is not .

Regards,
Mehmet

Please try below code

    using System.Drawing;
    using Aspose.Gis.Geometries;
    using Aspose.Gis.Rendering;
    using Aspose.Gis;
    using Point = Aspose.Gis.Geometries.Point;
    using Aspose.Gis.Rendering.Symbolizers;

    public string RenderGeometrySelfColor()
    {
        // Get available file name
        var fileName = "output";
        var fileExtension = ".png";
        var availableFileName = fileName;
        int j = 0;
        while (System.IO.File.Exists(availableFileName + fileExtension))
        {
            availableFileName = fileName + j;
            j++;
        }

        // Create a geometry collection with different geometries
        var geometryCollection = new GeometryCollection
        {
            new Point(10, 20),
            new LineString(new[] {new Point(30, 40), new Point(50, 60)}),
            new Polygon(new LinearRing(new[] {new Point(70, 80), new Point(110, 120), new Point(100, 120), new Point(70, 80)}))
        };

        // Define colors for each geometry
        var colors = new[] { Color.Red, Color.Green, Color.Blue };

        // Create in memory layer to map each geometry with its respective color
        var layer = Drivers.InMemory.CreateLayer();
        layer.Attributes.Add(new FeatureAttribute("Color", AttributeDataType.Integer));
        for (int i = 0; i < geometryCollection.Count; i++)
        {
            // Create a feature and add it to the layer
            var feature = layer.ConstructFeature();
            feature.SetValue("Color", colors[i].ToArgb());
            feature.Geometry = geometryCollection[i];
            layer.Add(feature);
        }

        // Create a new map instance
        using (var map = new Aspose.Gis.Rendering.Map(200, 200))
        {
            map.Padding = 5;

            var pointSymbolizer = new SimpleMarker
            {
                FeatureBasedConfiguration = (feature, symbolizer) =>
            {
                int color = feature.GetValue<int>("Color");
                symbolizer.FillColor = Color.FromArgb(color);
            }
            };

            var lineSymbolizer = new SimpleLine
            {
                FeatureBasedConfiguration = (feature, symbolizer) =>
                {
                    int color = feature.GetValue<int>("Color");
                    symbolizer.Color = Color.FromArgb(color);
                }
            };

            var poligonSymbolizer = new SimpleFill
            {
                FeatureBasedConfiguration = (feature, symbolizer) =>
                {
                    int color = feature.GetValue<int>("Color");
                    symbolizer.FillColor = Color.FromArgb(color);
                }
            };

            map.Add(layer, new MixedGeometrySymbolizer() { PointSymbolizer = pointSymbolizer, LineSymbolizer = lineSymbolizer, PolygonSymbolizer = poligonSymbolizer });
            map.Render(availableFileName + fileExtension, Renderers.Jpeg);
        }

        return availableFileName + fileExtension;
    }

Best Regards

1 Like

Hi @Evgeniy.Timofeev,

Thank you for your input , I see where I went wrong :slight_smile:.
I have tweaked your code a bit at the end; and used a abstract path to render the result to a imagestream to use this without having to save to file first.

        var imageStream = new MemoryStream();
        AbstractPath path = AbstractPath.FromStream(imageStream);

        //map.Render(availableFileName + fileExtension, Renderers.Png);
        
        map.Render(path, Renderers.Png);

        var byteArray = imageStream.ToArray();
        var bytes = Convert.ToBase64String(byteArray);
        retPng = String.Format("data:image/png;base64,{0}", bytes);

Thank you again, this helped me a lot .

Regards,
Mehmet

1 Like