JackYumCha / QA

Question and Answer
0 stars 0 forks source link

Convert GeoJson to CSV file #26

Open JackYumCha opened 6 years ago

JackYumCha commented 6 years ago
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Xunit;
using Newtonsoft.Json;
using GeoJSON.Net.Geometry;
using GeoJSON.Net.Feature;
using GeoJSON.Net;
using System.Linq;
using System.IO;
using CsvHelper;

namespace ConvertGeoJsonToWKT
{
    public class UnitTest1
    {
        [Fact(DisplayName = "Convert Congressional GeoJson to Wkt")]
        public void ConvertCongressional()
        {
            var fileCongressionals = @"C:\Users\erris\Desktop\Lecture\Chapter 9\US Congressional.json";
            var json = File.ReadAllText(fileCongressionals);
            var root = JsonConvert.DeserializeObject<FeatureCollection>(json);
            var congressionals =  root.Features.Select((feature, index)=>
            {
                CongressionalGeoData congressionalGeoData = new CongressionalGeoData();
                congressionalGeoData.Id = feature.Properties["GEO_ID"] as string;

                Polygon polygon = feature.Geometry as Polygon;
                MultiPolygon multipolygon = feature.Geometry as MultiPolygon;
                if(polygon != null)
                {
                    congressionalGeoData.WKT = polygon.toWKT();
                }
                else if(multipolygon != null)
                {
                    congressionalGeoData.WKT = multipolygon.toWKT();
                }
                else{
                    Debugger.Break();
                }
                return congressionalGeoData;
            });

            string targetFile = @"C:\Users\erris\Desktop\Lecture\Chapter 9\congressional.csv";

            using(StreamWriter sw = new StreamWriter(targetFile))
            {
                using(CsvWriter csv = new CsvWriter(sw))
                {
                    csv.WriteField("id");
                    csv.WriteField("wkt");
                    csv.NextRecord();

                    foreach(var cgd in congressionals)
                    {
                        csv.WriteField(cgd.Id);
                        csv.WriteField(cgd.WKT);
                        csv.NextRecord();
                    }

                }
            }

            Debugger.Break();
        }

        [Fact(DisplayName = "Convert Counties GeoJson to Wkt")]
        public void ConvertCounties()
        {

            var fileCounties = @"C:\Users\erris\Desktop\Lecture\Chapter 9\US Counties.json";
            var json = File.ReadAllText(fileCounties);
            var root = JsonConvert.DeserializeObject<FeatureCollection>(json);
            var congressionals = root.Features.Select((feature, index) =>
            {
                CongressionalGeoData congressionalGeoData = new CongressionalGeoData();
                congressionalGeoData.Id = $@"{feature.Properties["GEO_ID"]}-{feature.Properties["STATE"]}-{feature.Properties["NAME"]}";

                Polygon polygon = feature.Geometry as Polygon;
                MultiPolygon multipolygon = feature.Geometry as MultiPolygon;
                if (polygon != null)
                {
                    congressionalGeoData.WKT = polygon.toWKT();
                }
                else if (multipolygon != null)
                {
                    congressionalGeoData.WKT = multipolygon.toWKT();
                }
                else
                {
                    Debugger.Break();
                }
                return congressionalGeoData;
            });

            string targetFile = @"C:\Users\erris\Desktop\Lecture\Chapter 9\counties.csv";

            using (StreamWriter sw = new StreamWriter(targetFile))
            {
                using (CsvWriter csv = new CsvWriter(sw))
                {
                    csv.WriteField("id");
                    csv.WriteField("wkt");
                    csv.NextRecord();

                    foreach (var cgd in congressionals)
                    {
                        csv.WriteField(cgd.Id);
                        csv.WriteField(cgd.WKT);
                        csv.NextRecord();
                    }

                }
            }

            Debugger.Break();
        }
    }

    public static class WKTExtensions
    {
        public static string toWKT(this Polygon polygon)
        {
            return $@"POLYGON ({string.Join(",", polygon.Coordinates.Select(linestring =>
            {
                return $@"({string.Join(",", linestring.Coordinates.Select(point => $"{point.Longitude} {point.Latitude}"))})";
            }))})";
        }

        public static string toWKT(this MultiPolygon multiPolygon)
        {
            return $@"MULTIPOLYGON ({string.Join(",", multiPolygon.Coordinates.Select(polygon =>
            {
                return $@"({string.Join(",", polygon.Coordinates.Select(linestring => {
                    return $@"({string.Join(",", linestring.Coordinates.Select(point => $"{point.Longitude} {point.Latitude}"))})";
                }))})";
            }))})";
        }
    }

    public class CongressionalGeoData
    {
        public string Id { get; set; }
        public string WKT { get; set; }
    }
}