snippetcsharpMinor
Convert WKT representations of geometries into GeoJson
Viewed 0 times
convertintorepresentationswktgeojsongeometries
Problem
I am working on a class library in C# to convert
```
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace WKT
{
class Convert
{
private const string GEOMETRYCOLLECTION = "GeometryCollection";
private const string LINESTRING = "LineString";
private const string MULTILINESTRING = "MultiLineString";
private const string MULTIPOINT = "MultiPoint";
private const string MULTIPOLYGON = "MultiPolygon";
private const string POINT = "Point";
private const string POLYGON = "Polygon";
static public string ToGeoJSON(Dictionary geos)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue("FeatureCollection");
WriteFeatures(writer, geos);
writer.WriteEndObject();
return sb.ToString();
}
}
static private void WriteFeatures(JsonWriter writer, Dictionary geos)
{
writer.WritePropertyName("features");
writer.WriteStartArray();
foreach (KeyValuePair geo in geos)
{
WriteFeature(writer, geo.Key, geo.Value);
}
writer.WriteEndArr
WKT to GeoJson. I am getting shapes in WKT out of MSSQL. A lot of the shapes that come out of SQL have a lot of points. I am concerned that even though what I have is working on the much smaller shapes I am using for testing, that it will either fail or be really slow on larger sets. I am also not sure if regular expressions are the way to go especially when trying to convert some of the more complex WKT objects like multiPolygon.```
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
namespace WKT
{
class Convert
{
private const string GEOMETRYCOLLECTION = "GeometryCollection";
private const string LINESTRING = "LineString";
private const string MULTILINESTRING = "MultiLineString";
private const string MULTIPOINT = "MultiPoint";
private const string MULTIPOLYGON = "MultiPolygon";
private const string POINT = "Point";
private const string POLYGON = "Polygon";
static public string ToGeoJSON(Dictionary geos)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue("FeatureCollection");
WriteFeatures(writer, geos);
writer.WriteEndObject();
return sb.ToString();
}
}
static private void WriteFeatures(JsonWriter writer, Dictionary geos)
{
writer.WritePropertyName("features");
writer.WriteStartArray();
foreach (KeyValuePair geo in geos)
{
WriteFeature(writer, geo.Key, geo.Value);
}
writer.WriteEndArr
Solution
The
That doesn't really address any performance concerns though.
ConvertGeometryType function could be rewritten a little bit. Instead of breaking from the switch and returning value, you could directly return and use a default case to make sure you get an empty string if none of the cases matched. switch (GeometryType)
{
case "GEOMETRYCOLLECTION":
return GEOMETRYCOLLECTION;
// bunch of other case statements
default:
return "";
}That doesn't really address any performance concerns though.
Code Snippets
switch (GeometryType)
{
case "GEOMETRYCOLLECTION":
return GEOMETRYCOLLECTION;
// bunch of other case statements
default:
return "";
}Context
StackExchange Code Review Q#56591, answer score: 2
Revisions (0)
No revisions yet.