static int GetSrid(ICRSObject crs) { if (crs == null || crs is UnspecifiedCRS) { return(0); } var namedCrs = crs as NamedCRS; if (namedCrs == null) { throw new NotSupportedException("The LinkedCRS class isn't supported"); } if (namedCrs.Properties.TryGetValue("name", out var value) && value != null) { var name = value.ToString(); if (string.Equals(name, "urn:ogc:def:crs:OGC::CRS84", StringComparison.Ordinal)) { return(4326); } var index = name.LastIndexOf(':'); if (index != -1 && int.TryParse(name.Substring(index + 1), out var srid)) { return(srid); } throw new FormatException("The specified CRS isn't properly named"); } return(0); }
/// <summary> /// Writes a coordinate reference system to its JSON representation /// </summary> /// <param name="writer">The writer</param> /// <param name="value">The coordinate reference system</param> /// <param name="serializer">The serializer</param> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (writer == null) { throw new ArgumentNullException("writer"); } if (serializer == null) { throw new ArgumentNullException("serializer"); } ICRSObject crs = value as ICRSObject; if (crs == null) { writer.WriteToken(JsonToken.Null); return; } writer.WriteStartObject(); writer.WritePropertyName("type"); string type = Enum.GetName(typeof(CRSTypes), crs.Type); writer.WriteValue(type.ToLowerInvariant()); CRSBase crsb = value as CRSBase; if (crsb != null) { writer.WritePropertyName("properties"); serializer.Serialize(writer, crsb.Properties); } writer.WriteEndObject(); }
/// <summary> /// Convert Vector3 World Space location to Position taking account of zoom, scale and mapscale /// </summary> /// <param name="position">Vector3 World Space coordinates</param> /// <returns>Position</returns> static public IPosition ToPosition(this Vector3 position, ICRSObject crs = null) { Geometry geom = position.ToGeometry(); SpatialReference sr = new SpatialReference(null); if (crs == null) { sr.SetWellKnownGeogCS("EPSG:4326"); } else { switch (crs.Type) { case CRSType.Name: string name = (crs as NamedCRS).Properties["name"] as string; sr.SetProjCS(name); break; case CRSType.Link: string url = (crs as LinkedCRS).Properties["href"] as string; sr.ImportFromUrl(url); break; case CRSType.Unspecified: sr.SetWellKnownGeogCS("EPSG:4326"); break; } } geom.TransformTo(sr); double[] argout = new double[3]; geom.GetPoint(0, argout); return(new Position(argout[0], argout[1], argout[2])); }
/// <summary> /// Converts Iposition to Vector3 World Space coordinates takling account of zoom, scale and mapscale /// </summary> /// <param name="position">IPosition</param> /// <returns>Vector3</returns> public static Vector3 Vector3(this IPosition position, ICRSObject crs = null) { if (crs == null) { crs = new NamedCRS("EPSG:4326"); } return(position.ToGeometry(crs).TransformWorld()[0]); }
/// <summary> /// Determines whether the specified object is equal to the current object /// </summary> public bool Equals(ICRSObject obj) { if (ReferenceEquals(this, obj)) { return(true); } if (ReferenceEquals(null, obj)) { return(false); } return(Type == obj.Type); }
/// <summary> /// Determines whether the specified object is equal to the current object /// </summary> public bool Equals(ICRSObject obj) { if (ReferenceEquals(this, obj)) { return true; } if (ReferenceEquals(null, obj)) { return false; } return Type == obj.Type; }
static public Geometry ToGeometry(this LineString line) { Geometry geom = new Geometry(wkbGeometryType.wkbLineString); SpatialReference sr = new SpatialReference(null); ICRSObject crs = line.CRS; if (crs == null) { crs = new NamedCRS("EPSG:4326"); } switch (crs.Type) { case CRSType.Name: string name = (crs as NamedCRS).Properties["name"] as string; if (name.Contains("urn")) { sr.ImportFromUrl(name); } else if (name.Contains("EPSG")) { string[] args = name.Split(':'); sr.ImportFromEPSG(int.Parse(args[1])); } else { sr.SetWellKnownGeogCS(name); } break; case CRSType.Link: string url = (crs as LinkedCRS).Properties["href"] as string; sr.ImportFromUrl(url); break; case CRSType.Unspecified: sr.SetWellKnownGeogCS("EPSG:4326"); break; } geom.AssignSpatialReference(sr); Position[] vertexes = line.Points(); foreach (Position vertex in vertexes) { Nullable <double> alt = vertex.Altitude; geom.AddPoint(vertex.Latitude, vertex.Longitude, alt ?? 0.0); } return(geom); }
/// <summary> /// Tries to finds the coordinate system crs property used. /// </summary> /// <param name="crsObject">The crs object.</param> /// <returns>The coordinate system id used. If no crs was found, None is returned.</returns> public CoordinateSystemId FindCoordinateSystemId(ICRSObject crsObject) { if (crsObject.Type == CRSType.Link) { // We don't support link conversion return(CoordinateSystemId.None); } if (crsObject.Type == CRSType.Name) { return(FindCoordinateSystemId((NamedCRS)crsObject)); } if (crsObject.Type == CRSType.EPSG) { var crs = (EPSGCRS)crsObject; switch (crs.Code) { case 3006: return(CoordinateSystemId.SWEREF99_TM); case 2400: case 3021: return(CoordinateSystemId.Rt90_25_gon_v); case 900913: case 3857: return(CoordinateSystemId.GoogleMercator); case 4619: return(CoordinateSystemId.SWEREF99); case 4326: return(CoordinateSystemId.WGS84); default: return(CoordinateSystemId.None); } } throw new ArgumentException(string.Format("{0} is not supported.", crsObject.Type)); }
private void SerializeLinkedCRS(ICRSObject crs) { var linkedCrs = (LinkedCRS) crs; Writer.WritePropertyName("type"); Writer.WriteValue("link"); Writer.WritePropertyName("properties"); Writer.WriteStartObject(); Writer.WritePropertyName("href"); Writer.WriteValue((string) linkedCrs.Properties["href"]); //object boxed string object typeProperty; if (linkedCrs.Properties.TryGetValue("type", out typeProperty)) { Writer.WritePropertyName("type"); Writer.WriteValue((string) typeProperty); //object boxed string } Writer.WriteEndObject(); }
private void SerializeNamedCRS(ICRSObject crs) { var namedCrs = (NamedCRS) crs; Writer.WritePropertyName("type"); Writer.WriteValue("name"); Writer.WritePropertyName("properties"); Writer.WriteStartObject(); Writer.WritePropertyName("name"); Writer.WriteValue((string) namedCrs.Properties["name"]); //object boxed string Writer.WriteEndObject(); }
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); }