Getting started

edit

Install the package

Add the Meziantou.Framework.Geo package to a project:

dotnet package add Meziantou.Framework.Geo

The examples of this documentation omit the using directives: they need using Meziantou.Framework.Geo;.

Read and write a file

Every format has a static class with the same methods: Read reads a stream, Parse reads a string, Write writes to a stream and Format returns a string. This example reads the countries of a GeoJSON file with GeoJson.Read, keeps the large ones and writes them to another file with GeoJson.Write:

GeoFeatureCollection countries;
using (var stream = File.OpenRead("countries.geojson"))
{
    countries = GeoJson.Read(stream);
}

// A JSON number is a double, and a string is a string
var large = countries.Where(country => (double)country.Properties["population"]! > 50).ToList();
Assert.Equal("France", Assert.Single(large).Properties["name"]);

using (var stream = File.Create("large-countries.geojson"))
{
    GeoJson.Write(new GeoFeatureCollection(large), stream, new GeoJsonWriterOptions { Indented = true });
}
  • A reader returns a GeoFeatureCollection: a list of GeoFeature, each with a geometry, properties and an identifier.
  • The readers and writers do not own the streams: dispose them yourself.
  • Invalid data throws a FormatException.

To convert a file to another format, read it with one class and write it with another: see formats.

Next steps