/// <summary> /// 将传入的JSONstring型geometry转化为指定格式 /// </summary> /// <param name="inString">JSONstring型geometry</param> /// <param name="outType">输出的geometry类型</param> /// <returns>Hashtable 或 string</returns> public static object Transform(string inString, objectType outType) { object ht; try { geometryEntity geo = formatToEntity(inString); ht = formatToGeometry(geo, outType); } catch (Exception e) { throw e; } return(ht); }
/// <summary> /// 将传入的Hashtable转换为指定格式的geometry /// </summary> /// <param name="inHastTable">传入的Hashtable</param> /// <param name="outType">Enum 输出的geometry类型</param> /// <returns>Hashtable 或 string</returns> public static object Transform(Hashtable inHastTable, objectType outType) { object ht; try { geometryEntity geo = formatToEntity(inHastTable); ht = formatToGeometry(geo, outType); } catch (Exception e) { throw e; } return(ht); }
/// <summary> /// 将geometries对象转化为指定layout的geometry /// </summary> /// <param name="geometry">geometry实体对象</param> /// <param name="layout">Enum 输出geometry类型</param> /// <returns>string 或 hashtable</returns> private static object formatToGeometry(geometryEntity geometry, objectType layout) { Hashtable resultht = new Hashtable(); string resultgeostr = ""; //转换为esriGeometries if (layout == objectType.esriGeometries) { Hashtable wkidht = new Hashtable(); wkidht.Add("wkid", geometry.wkid); //点 if (geometry.type == geometryType.point) { Hashtable geoht = new Hashtable(); geoht.Add("x", geometry.coordinates[0]); geoht.Add("y", geometry.coordinates[1]); geoht.Add("spatialReference", wkidht); ArrayList list = new ArrayList(); list.Add(geoht); resultht.Add("geometryType", "esriGeometryPoint"); resultht.Add("geometries", list); } //线 和 面 else if (geometry.type == geometryType.line || geometry.type == geometryType.polygon) { string geotype = ""; string geoname = ""; if (geometry.type == geometryType.line) { geotype = "esriGeometryPolyline"; geoname = "paths"; } else { geotype = "esriGeometryPolygon"; geoname = "rings"; } Hashtable geoht = new Hashtable(); geoht.Add(geoname, geometry.coordinates); geoht.Add("spatialReference", wkidht); ArrayList list = new ArrayList(); list.Add(geoht); resultht.Add("geometryType", geotype); resultht.Add("geometries", list); } resultgeostr = JSON.JsonEncode(resultht); } //转换为normalGeometries 或 simpleGeometries else if (layout == objectType.normalGeometries || layout == objectType.simpleGeometries) { Hashtable wkidht = new Hashtable(); wkidht.Add("wkid", geometry.wkid); //点 if (geometry.type == geometryType.point) { if (layout == objectType.normalGeometries) { resultht.Add("type", "point"); } resultht.Add("x", geometry.coordinates[0]); resultht.Add("y", geometry.coordinates[1]); resultht.Add("spatialReference", wkidht); } //线 和 面 else if (geometry.type == geometryType.line || geometry.type == geometryType.polygon) { string geotype = ""; string geoname = ""; string _name = ""; if (geometry.type == geometryType.line) { geotype = "polyline"; geoname = "paths"; _name = "_path"; } else { geotype = "polygon"; geoname = "rings"; _name = "_ring"; } if (layout == objectType.normalGeometries) { resultht.Add("type", geotype); } resultht.Add(geoname, geometry.coordinates); if (layout == objectType.normalGeometries) { resultht.Add(_name, "0"); } resultht.Add("spatialReference", wkidht); if (layout == objectType.normalGeometries && geometry.type == geometryType.polygon) { resultht.Add("_centroid", "null"); resultht.Add("_extent", "null"); } } resultgeostr = JSON.JsonEncode(resultht); } //转化为STGeometries else if (layout == objectType.STGeometries) { //点 if (geometry.type == geometryType.point) { resultgeostr = $"POINT({geometry.coordinates[0]} {geometry.coordinates[1]})"; } //线 else if (geometry.type == geometryType.line) { string pointstr = ""; //循环线 for (int i = 0; i < geometry.coordinates.Count; i++) { ArrayList linelist = geometry.coordinates[i] as ArrayList; pointstr += "("; //循环线内点 for (int ii = 0; ii < linelist.Count; ii++) { pointstr += $"{(linelist[ii] as ArrayList)[0]} {(linelist[ii] as ArrayList)[1]},"; } pointstr = pointstr.TrimEnd(','); pointstr += "),"; } pointstr = pointstr.TrimEnd(','); if (geometry.coordinates.Count > 1) { resultgeostr = "MULTILINESTRING(" + pointstr + ")"; } else { resultgeostr = "LINESTRING" + pointstr; } } //面 else if (geometry.type == geometryType.polygon) { string pointstr = ""; //循环线 for (int i = 0; i < geometry.coordinates.Count; i++) { ArrayList linelist = geometry.coordinates[i] as ArrayList; pointstr += "(("; //循环线内点 for (int ii = 0; ii < linelist.Count; ii++) { pointstr += $"{(linelist[ii] as ArrayList)[0]} {(linelist[ii] as ArrayList)[1]},"; } pointstr = pointstr.TrimEnd(','); pointstr += ")),"; } pointstr = pointstr.TrimEnd(','); if (geometry.coordinates.Count > 1) { resultgeostr = "MULTIPOLYGON(" + pointstr + ")"; } else { resultgeostr = "POLYGON" + pointstr; } } } //转换为hashTableGeometries else if (layout == objectType.hashTableGeometries) { //面 或 线 if (geometry.type == geometryType.polygon || geometry.type == geometryType.line) { string typename = ""; string name = ""; string _name = ""; if (geometry.type == geometryType.polygon) { typename = "polygon"; name = "rings"; _name = "_ring"; } else { typename = "polyline"; name = "paths"; _name = "_path"; } Hashtable wkidht = new Hashtable(); wkidht.Add("wkid", geometry.wkid); wkidht.Add("latestWkid", geometry.wkid); Hashtable geoht = new Hashtable(); geoht.Add("type", typename); geoht.Add(name, geometry.coordinates); geoht.Add(_name, "0"); geoht.Add("spatialReference", wkidht); resultht.Add("geometry", geoht); } //点 else if (geometry.type == geometryType.point) { Hashtable wkidht = new Hashtable(); wkidht.Add("wkid", geometry.wkid); wkidht.Add("latestWkid", geometry.wkid); Hashtable geoht = new Hashtable(); geoht.Add("type", "point"); geoht.Add("x", geometry.coordinates[0]); geoht.Add("y", geometry.coordinates[1]); geoht.Add("spatialReference", wkidht); resultht.Add("geometry", geoht); } return(resultht); } return(resultgeostr); }