Convert Shapefiles to GeoJSON in C#

somegenericdev
3 min readNov 27, 2021

Shapefiles are files that contain geospatial data. They’re especially used together with GIS software like QGIS.
There are a number of reasons why we might want to convert them to GeoJSON, which is another format for geospatial data. One, for example, is that online tools like Leaflet usually exclusively accept GeoJSON. In this article we’ll investigate the challenges of converting Shapefiles to the GeoJSON format programmatically with C#.

Insert Nuget part here

The tools

There are several libraries in Python, like GeoPandas, that allow us to deal with geospatial data in a very straightforward way. Luckily, there is an easy way to call Python code from C#: PythonNET (I’ve written another article about the subject in the past, feel free to read it if you’re interested).
A thing to notice is that, at the moment when this article was written (November 2021), PythonNET supports only Python 3.6–3.8. If you have a more recent version of Python installed, you have to make sure to uninstall it and install Python 3.8 instead before proceeding.

The code

Python

Let’s start by working on the “Python side”.
We’ll need to install GeoPandas first. To do so, open the CMD and cd to the Scripts folder of your Python installation. For me the path looks kinda like “C:\Users\USERNAME\AppData\Local\Programs\Python\Python38\Scripts” but for you it might be different so look for it.
After that, launch these commands.

Now that GeoPandas was successfully installed, open your Python IDE of choice (I use VSCode but you can use whatever you want) and copypaste this script into it.

Now, to test the script, download this archive and extract the “Reg01012021_g” folder somewhere. That folder contains the shapefiles for the Italian regions. Let’s insert the path to our “Reg01012021_g” folder in the “path” variable of the script and then let’s try to run it (on VSCode, you have to press F1, set the language to Python and then press F5).
If everything went well, an output.json file was generated inside of our folder. Now go to https://geojson.io/ (preferably with Edge, Chrome can be buggy with this site in my experience), click “Open” → “File” and select the json file.

If the conversion happened successfully, something like this should show up on the map.

C#

Now, we need to build our C# wrapper.

Let’s download the source code of PythonNET, open the .sln file with Visual Studio and compile the solution. A file called Python.Runtime.dll will be generated. We need to reference that from our C# project.
Now, let’s create a class and call it PythonInterop.cs. Copypaste the code below into it. Make sure to replace the path of the pythonDll variable with the path your python installation that might be different!

Let’s also create a class called GeoConverter.cs that will contain a slightly modified version of the script we ran before. This time the script will save the GeoJSON into a string variable rather than saving it on the disk.

Let’s test our code

To test our code, we’ll create a Blazor Server project. Create an Index.razor.cs file that will be the code-behind of our homepage, and copypaste this code.

To allow us to successfully save the file, create a JS file in the wwwroot folder and paste this code.

Then add the script to the Host.cshtml file (make sure the path is correct for you)

Now we can take the shapefiles we were using before and convert them into a .zip archive that should look kind of like this.

Let’s run our Blazor app, upload the archive we just created and the generated GeoJSON file should be saved right away.

Source code

You can download the source code of the project from here.

--

--

somegenericdev

I like experimenting with Blazor and .NET and posting about my findings.