I've downloaded from openstreetmaps administrative borders (city, region, country, etc...) in geoJson format.
I'm trying to store the json data related to the polygons in my ms sql server using spatial data. Since I've never parsed such a complex json file I've got difficulties in manually creating a c# object to store the parsed data.
I'm using newtonsoftJson.json, to read the geoJson file and create an object. Then I use reflection to get the properties related to the object. This is quite a tedious task because in some cases the objects are nested in more than 4 levels in the geojson file. Since I've got to store the polygons for all the cities in the world, I clearly need a faster and accurate way of doing it.
- What could be the best method of achieving my goal?
- I guess json schemas could be of great help, but how?
- Is there a standard geoJson schema, in the sense that if I use the schema for openstreetmaps and an other GIS framework (google maps), will it be compatible?
In SQL Server 2016 you can parse GeoJSON format using OPENJSON function and then you can convert table of coordinates to spatial types. Each nested GeoJSON level is handled with one additional CROSS APPLY call. Also, you can load GeoJSON into spatial columns If you don't use SQL Server 2016 or you want to use logic in app layer, then probably the only way is Json.Net.
This can be done in pure SQL in SqlServer 2016, as @javan-MSFT has answered in 2015. Doing this in SQL means there are no runtime dependencies, you can easily upload static data records directly in SSMS, but I tell you what it's not as easy as it perhaps should be, the links in MSDN blogs are a basic start but wont help you in a hurry:
Without posting a snippet of your dataset, I can't tailor a query that will work 100% but I can show you using a snippet of data that I sourced from https://github.com/johan/world.geo.json This is a good low res GeoJSON file, hi res files follow the same structure just have a lot more points.
I've used APPLY queries here to hopefully make the code a bit easier to understand but also to make it easier for you to inspect the logic and customise it to your needs.
MultiPolygon lines were a lot harder to get right, its only 1 additional dimension over a normal polygon but still takes a bit to wrap your head around.