Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/2da6c458edc3e9005980432ae333d3f6 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/2da6c458edc3e9005980432ae333d3f6 to your computer and use it in GitHub Desktop.
Convert GeoJson to TopoJson or TopoJSON to GeoJSON programmatically using C#
// convert GeoJSON to TopoJSON with quantization - transforming doubles to integers to reduce file size.
string SampleGeoJsonPath = dataDir + "sample.geojson";
var outputFilePath = dataDir + "convertedSampleWithQuantization_out.topojson";
// Initialize ConversionOptions class object
var options = new ConversionOptions
{
DestinationDriverOptions = new TopoJsonOptions
{
// There are two ways to set quantization parameters - with Transform property or
// with quantization number. Here we specify quantization number.
// Quantization number specifies number of expressible values per dimension in a result
// coordinates.
QuantizationNumber = 100_000,
// Alternatively, 'Transform' property can be set (but not simultaneously with quantization number).
// Refer to TopoJSON specification for more details on transform object and quantization.
//
// Transform = new TopoJsonTransform(
// xTranslate: 0,
// yTranslate: 0,
// xScale: 0.0001000010000100001,
// yScale: 0.0001000010000100001),
}
};
VectorLayer.Convert(SampleGeoJsonPath, Drivers.GeoJson, outputFilePath, Drivers.TopoJson, options);
// Load input GeoJSON file
string sampleGeoJsonPath = dataDir + "sample.geojson";
var outputFilePath = dataDir + "convertedSample_out.topojson";
// convert GeoJSON to TopoJSON
VectorLayer.Convert(sampleGeoJsonPath, Drivers.GeoJson, outputFilePath, Drivers.TopoJson);
// Load input TopoJSON file
var sampleTopoJsonPath = dataDir + "sample.topojson";
var outputFilePath = dataDir + "convertedSample_out.geojson";
// Convert TopoJSON to GeoJSON file
VectorLayer.Convert(sampleTopoJsonPath, Drivers.TopoJson, outputFilePath, Drivers.GeoJson);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment