public static AnnotatedObject <Geometry>[] GetGeomsFromGeoJson(string geoJson) { List <AnnotatedObject <Geometry> > output = new List <AnnotatedObject <Geometry> >(); GeoJsonReader reader = new GeoJsonReader(GeometryFactory.Default, new JsonSerializerSettings()); ProtoGeoJSON obj = reader.Read <ProtoGeoJSON>(geoJson); AnnotatedObject <Geometry> annotatedGeom = new AnnotatedObject <Geometry>(null); Geometry geom; bool isFeatureCollection = false; switch (obj.Type) { case "Point": annotatedGeom.Object = reader.Read <Point>(geoJson); break; case "LineString": annotatedGeom.Object = reader.Read <LineString>(geoJson); break; case "Polygon": annotatedGeom.Object = reader.Read <NetTopologySuite.Geometries.Polygon>(geoJson); break; case "MultiPoint": annotatedGeom.Object = reader.Read <MultiPoint>(geoJson); break; case "MultiLineString": annotatedGeom.Object = reader.Read <MultiLineString>(geoJson); break; case "MultiPolygon": annotatedGeom.Object = reader.Read <MultiPolygon>(geoJson); break; case "Feature": Feature feature = reader.Read <Feature>(geoJson); annotatedGeom.Object = feature.Geometry; foreach (string attribute in feature.Attributes.GetNames()) { annotatedGeom.Data[attribute] = feature.Attributes[attribute]; } break; case "FeatureCollection": FeatureCollection collection = reader.Read <FeatureCollection>(geoJson); foreach (Feature item in collection) { annotatedGeom = new AnnotatedObject <Geometry>(item.Geometry); annotatedGeom.Object.Normalize(); foreach (string attribute in item.Attributes.GetNames()) { annotatedGeom.Data[attribute] = item.Attributes[attribute]; } output.Add(annotatedGeom); } isFeatureCollection = true; break; default: throw new NotImplementedException(string.Format("No handler found for type {0}", obj.Type)); } if (!isFeatureCollection) { annotatedGeom.Object.Normalize(); output.Add(annotatedGeom); } return(output.ToArray()); }
public static bool GetProjectionFromGeoJson(string fileName, out string projectionName, out string projection) { string geoJson = File.ReadAllText(fileName); ProtoGeoJSON obj = JsonConvert.DeserializeObject <ProtoGeoJSON>(geoJson); projectionName = string.Empty; projection = string.Empty; ICRSObject crs = null; switch (obj.Type) { case "FeatureCollection": GFeatureCollection features = JsonConvert.DeserializeObject <GFeatureCollection>(geoJson); crs = features.CRS; break; case "Feature": GFeature feature = JsonConvert.DeserializeObject <GFeature>(geoJson); crs = feature.CRS; break; case "MultiPolygon": GMultiPolygon multiPoly = JsonConvert.DeserializeObject <GMultiPolygon>(geoJson); crs = multiPoly.CRS; break; case "Polygon": GPolygon poly = JsonConvert.DeserializeObject <GPolygon>(geoJson); crs = poly.CRS; break; default: break; } if (crs is NamedCRS) { NamedCRS nCrs = crs as NamedCRS; object value; if (nCrs.Properties.TryGetValue("name", out value)) { string name = value as string; if (name != null) { name = name.ToLower(); if (name.EndsWith("3857")) { projectionName = "EPSG:3857 Pseudo-Mercator"; projection = CoordinateConverter.WebMercator.WKT; return(true); } else { int index = name.IndexOf("epsg::"); string epsgStr = name.Substring(index); string authNumber = epsgStr.Substring(epsgStr.IndexOf("::") + 2); projection = AutoProjection.GetProjection(authNumber); if (projection != string.Empty) { projectionName = string.Format("EPSG:{0}", authNumber); return(true); } } } } } projectionName = "EPSG:4326 Lat-Long"; projection = CoordinateConverter.WGS84.WKT; return(true); }