Skip to content

Extensibility Guide

Most ThinkGeo classes can be subclassed, and two extensions cover most needs: a feature source for a data format ThinkGeo does not read, and a style that draws in a way the built-in styles do not. The code on this page compiles against ThinkGeo 15; the How Do I samples show it running.

A feature source for your own format

A feature source needs two things: read the data when it opens, and hand back all of its features. The base class answers every other query, such as features inside a bounding box, the count or the extent, from those features. That is enough for a small file; for a large one, override GetFeaturesInsideBoundingBoxCore as well so a query reads only what it needs.

This one reads a CSV file of latitude,longitude lines:

using System.Collections.ObjectModel;
using System.Globalization;
using ThinkGeo.Core;

public class SimpleCsvFeatureSource : FeatureSource
{
    private Collection<Feature> features = new Collection<Feature>();

    public SimpleCsvFeatureSource(string csvPathFileName)
    {
        CsvPathFileName = csvPathFileName;
    }

    public string CsvPathFileName { get; }

    // Each line is "latitude,longitude", with a period as the decimal mark.
    protected override void OpenCore()
    {
        var read = new Collection<Feature>();
        foreach (var line in File.ReadAllLines(CsvPathFileName))
        {
            var parts = line.Split(',');
            read.Add(new Feature(double.Parse(parts[1], CultureInfo.InvariantCulture),
                                 double.Parse(parts[0], CultureInfo.InvariantCulture)));
        }
        features = read;
    }

    protected override Collection<Feature> GetAllFeaturesCore(IEnumerable<string> returningColumnNames)
    {
        return features;
    }
}

The new source works wherever a built-in one does. With a style document, it goes into a FeatureSourceVectorTileSource like a shapefile in the Desktop Maps Quick Start; the tiles are named after the key you give it:

var tiles = new FeatureSourceVectorTileSource();
tiles.FeatureSources.Add("points", new SimpleCsvFeatureSource(@"Data\points.csv")
{
    ProjectionConverter = new ProjectionConverter(4326, 3857)
});

With classic layers, a FeatureLayer that holds it is all it takes:

public class SimpleCsvFeatureLayer : FeatureLayer
{
    public SimpleCsvFeatureLayer(string csvPathFileName)
    {
        FeatureSource = new SimpleCsvFeatureSource(csvPathFileName);
    }

    public override bool HasBoundingBox => true;
}

A custom style

A style decides how each feature is drawn. Override DrawCore to draw the features, and GetRequiredColumnNamesCore to name the columns it reads, so the layer fetches them. This one draws a circle sized by a column's value:

using System.Collections.ObjectModel;
using System.Globalization;
using ThinkGeo.Core;

// A circle whose size in pixels is the column's value divided by Ratio.
public class SizedPointStyle : Style
{
    public PointStyle PointStyle { get; set; } = new PointStyle();

    public float Ratio { get; set; } = 1;

    public string SizeColumnName { get; set; } = string.Empty;

    protected override void DrawCore(IEnumerable<Feature> features, GeoCanvas canvas,
        Collection<SimpleCandidate> labelsInThisLayer, Collection<SimpleCandidate> labelsInAllLayers)
    {
        foreach (var feature in features)
        {
            if (!double.TryParse(feature.ColumnValues[SizeColumnName], NumberStyles.Float, CultureInfo.InvariantCulture, out var value)) continue;

            // A new PointStyle per feature: tiles draw in parallel, so the shared one must not change.
            var sized = new PointStyle(PointStyle.SymbolType, (int)Math.Round(value / Ratio), PointStyle.FillBrush, PointStyle.OutlinePen);
            sized.Draw(new Collection<Feature> { feature }, canvas, labelsInThisLayer, labelsInAllLayers);
        }
    }

    protected override Collection<string> GetRequiredColumnNamesCore()
    {
        var columns = PointStyle.GetRequiredColumnNames();
        if (!columns.Contains(SizeColumnName)) columns.Add(SizeColumnName);
        return columns;
    }
}

Add it to a zoom level's CustomStyles, like any classic style. A custom Style draws classic layers; in a style document the same effect is a data-driven expression, such as a circle-radius read from the feature, as Styling Your Data shows.