Converting GEOJSON files to TopoJSON is a common need for GIS developers who want smaller, topology‑aware datasets for web maps. Aspose.GIS for .NET is a powerful SDK that simplifies this conversion in C# applications. In this guide you will learn how to set up the environment, run an async streaming conversion, optimize performance for large files, and follow best‑practice recommendations, all backed by a complete code sample.
Steps to Perform GEOJSON to Topojson Conversion in .NET
- Install the Aspose.GIS NuGet package - run
Install-Package Aspose.GISin the Package Manager Console. - Create a
VectorLayerfrom the source GEOJSON - useVectorLayer.FromFile("input.geojson"). - Configure conversion options - set topology precision and target coordinate system via
TopoJsonExportOptions. - Execute the async export - call
ExportToTopoJsonAsync(outputStream, options)to write the result without loading the whole file into memory. - Handle the result - save the stream to a file or return it from a web API.
For detailed class information see the API reference.
GEOJSON to Topojson Sample - Complete Code Example
The following example demonstrates a full end‑to‑end conversion, including async streaming and optional coordinate‑system transformation.
using System;
using System.IO;
using System.Threading.Tasks;
using Aspose.Gis;
using Aspose.Gis.Export;
using Aspose.Gis.Geometries;
namespace GeoJsonToTopoJsonDemo
{
class Program
{
static async Task Main(string[] args)
{
// Path to the source GEOJSON file
string geoJsonPath = @"C:\Data\sample.geojson";
// Output TopoJSON file path
string topoJsonPath = @"C:\Data\sample.topojson";
// Load GEOJSON into a VectorLayer
using (VectorLayer geoLayer = VectorLayer.FromFile(geoJsonPath))
{
// Prepare export options
var options = new TopoJsonExportOptions
{
// Set topology precision (higher = more accurate, larger file)
Precision = 0.00001,
// Optional: reproject to WGS84 if needed
TargetCoordinateSystem = SpatialReferenceSystem.Wgs84
};
// Open a file stream for async writing
using (FileStream outputStream = new FileStream(topoJsonPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, useAsync: true))
{
// Perform async conversion
await geoLayer.ExportToTopoJsonAsync(outputStream, options);
}
}
Console.WriteLine("Conversion completed successfully.");
}
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
sample.geojson,sample.topojson) to match your actual locations, verify that all required dependencies are installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Installation and Setup in .NET
Open your Visual Studio solution.
Open Package Manager Console and run:
Install-Package Aspose.GISAlternatively, use the NuGet UI to search for Aspose.GIS and install it.
After installation, add
using Aspose.Gis;to your C# files.For a quick start, download the sample data from the download page.
Geojson to Topojson Conversion in .NET with Aspose.GIS
GeoJSON stores geographic features as simple JSON objects, while TopoJSON adds topology information, reducing file size and eliminating redundant coordinates. Converting to TopoJSON is especially useful for web‑mapping libraries such as D3.js or Leaflet, where bandwidth and rendering speed matter. Aspose.GIS for .NET handles the heavy lifting: it parses GeoJSON, builds a topology graph, and writes a standards‑compliant TopoJSON file in a single call.
Aspose.GIS Features That Matter For This Task
- Full support for GEOJSON and TOPJSON - native readers and writers.
- Async streaming API - process files larger than available RAM.
- Precision control - balance between file size and geometric accuracy.
- Coordinate‑system transformation - reproject on the fly during export.
- High‑performance topology engine - optimized for large feature collections.
Async Conversion and Streaming Support
The SDK’s async methods (ExportToTopoJsonAsync) let you work with streams, which is ideal for web services or background jobs. By using a FileStream with useAsync:true, the conversion runs on I/O‑bound threads, freeing the main thread and keeping the application responsive. You can also pipe the output directly to an HTTP response stream, eliminating temporary files.
Performance Optimization for Large GeoJSON Files
When dealing with multi‑megabyte GEOJSON files, consider the following tips:
| File Size | Approx. Conversion Time (seconds) | Memory Usage (MB) |
|---|---|---|
| 5 MB | 0.8 | 45 |
| 50 MB | 5.2 | 210 |
| 200 MB | 21.5 | 780 |
Tips:
- Increase
Precisiononly as needed - lower precision speeds up processing. - Use the async streaming API to keep memory footprint low.
- Run the conversion on a background thread or a dedicated worker service.
Best Practices and Code Samples
- Validate input - call
geoLayer.Validate()before exporting to catch malformed geometries. - Reuse
TopoJsonExportOptions- create a single options instance if converting many files with the same settings. - Handle exceptions - wrap the export call in a try‑catch block and log
Aspose.Gis.GisExceptiondetails. - Dispose resources - always use
usingstatements forVectorLayerand streams to release file handles promptly. - Version control - keep the Aspose.GIS NuGet package version consistent across environments to avoid API mismatches.
Conclusion
This guide has shown how to perform GEOJSON to Topojson conversion in .NET using Aspose.GIS for .NET. You now have a working async implementation, performance‑tuning strategies for large datasets, and a set of best practices to keep your code robust. Remember to obtain a proper license for production use; you can start with a temporary license and later purchase a full license from the pricing page. Happy mapping!
FAQs
What is GEOJSON to Topojson conversion in .NET?
It is the process of reading a GEOJSON file, building a topology graph, and writing the result as a TopoJSON file using the Aspose.GIS for .NET SDK. The conversion reduces file size and preserves shared edges, which is ideal for web‑based visualizations.
Does Aspose.GIS support batch conversion of multiple GEOJSON files?
Yes. By looping over a collection of file paths and reusing a single TopoJsonExportOptions instance, you can convert many files efficiently. The async streaming API ensures low memory consumption even for large batches.
Can I convert GEOJSON to TopoJSON without writing to disk?
Absolutely. Use a MemoryStream or any other Stream implementation with ExportToTopoJsonAsync. This is useful for web APIs that need to return the TopoJSON directly to the client.
Is there a way to customize the topology precision?
The TopoJsonExportOptions.Precision property lets you define the decimal precision of coordinates. Lower values produce smaller files but may lose detail; adjust based on your application’s accuracy requirements.
