/// <summary> /// IGeometry转成JSON字符串 /// </summary> public static string GeometryToJsonString(IGeometry geometry) { IJSONWriter jsonWriter = new JSONWriterClass(); jsonWriter.WriteToString(); JSONConverterGeometryClass jsonCon = new JSONConverterGeometryClass(); jsonCon.WriteGeometry(jsonWriter, null, geometry, false); return(Encoding.UTF8.GetString(jsonWriter.GetStringBuffer())); }
/// <summary> /// JSON字符串转成IGeometry /// </summary> public static IGeometry ConvertToGeometry(string strJson, esriGeometryType type, bool bHasZ, bool bHasM) { IJSONReader jsonReader = new JSONReaderClass(); jsonReader.ReadFromString(strJson); JSONConverterGeometryClass jsonCon = new JSONConverterGeometryClass(); return(jsonCon.ReadGeometry(jsonReader, type, bHasZ, bHasM)); }
public static string ConvertGeometryToJson(IGeometry geometry, bool isGeneralize = false) { string geomJsonStr = null; try { ITopologicalOperator topoGeom = geometry as ITopologicalOperator; topoGeom.Simplify(); //IPolygon polygon = topoGeom as IPolygon; //if (polygon != null) //{ // polygon.Generalize(1); //} //if (geometry.SpatialReference == null || geometry.SpatialReference.Name == "Unknown") //{ // geometry.SpatialReference = SpatialRefOpt.GetProjectedCoordinate(esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_CM_120E); //} if (isGeneralize) { ISpatialReference spatialReference = geometry.SpatialReference; enumSpatialRelType spatialRelType = GetSpatialRelType(spatialReference); double offset = 0.000001; if (spatialRelType == enumSpatialRelType.GCS) { offset = 0.00000001; } IPolycurve polycurve = geometry as IPolycurve; polycurve.Generalize(offset); } IJSONWriter jsonWriter = new JSONWriterClass(); jsonWriter.WriteToString(); JSONConverterGeometryClass jsonCon = new JSONConverterGeometryClass(); jsonCon.WriteGeometry(jsonWriter, null, geometry, false); geomJsonStr = Encoding.UTF8.GetString(jsonWriter.GetStringBuffer()); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("\nConvertGeometryToJson::error::" + ex.Source + ".\n" + ex.ToString()); } return(geomJsonStr); }
/// <summary> /// 将json字符串转为Geometry对象 /// </summary> /// <param name="json"></param> /// <returns></returns> public static IGeometry Parse(string json, bool bHasZ = false, bool bHasM = false) { IGeometry result; if (string.IsNullOrWhiteSpace(json)) { return(null); } else { esriGeometryType type = GetGeometryType(json); IJSONReader jsonReader = new JSONReaderClass(); jsonReader.ReadFromString(json); JSONConverterGeometryClass jsonCon = new JSONConverterGeometryClass(); result = jsonCon.ReadGeometry(jsonReader, (esriGeometryType)type, bHasZ, bHasM); ITopologicalOperator topo = result as ITopologicalOperator; topo.Simplify(); } return(result); }
/** * Returns a list of customers serviced by the specified transformer */ private byte[] GetCustomersOperHandler(NameValueCollection boundVariables, JsonObject operationInput, string outputFormat, string requestProperties, out string responseProperties) { responseProperties = null; if (unDataset == null) { throw new RestErrorException("No Utility Network found"); } JSONObject result = new JSONObject(); try { string xfrAssetID = ""; operationInput.TryGetString("transformerAssetId", out xfrAssetID); if (string.IsNullOrEmpty(xfrAssetID)) { long?assetNumber = null; operationInput.TryGetAsLong("transformerAssetId", out assetNumber); if (assetNumber == null) { throw new ArgumentNullException("transformerAssetId"); } else { xfrAssetID = assetNumber.ToString(); } } // Get the transformer feature global ID from the asset ID string xfrWhereClause = "ASSETGROUP=" + MV_XFR_ASSETGROUP.ToString() + " AND ASSETID=" + xfrAssetID; IStringArray xfrOutFields = new StrArrayClass(); xfrOutFields.Add("GLOBALID"); xfrOutFields.Add("ASSETTYPE"); xfrOutFields.Add("SHAPE"); IPropertySetArray xfrQryResults = QueryDevices(xfrWhereClause, xfrOutFields); if (xfrQryResults == null || xfrQryResults.Count == 0) { throw new RestErrorException("No medium voltage transformer found for asset id: " + xfrAssetID); } string xfrGlobalID = (string)xfrQryResults.Element[0].GetProperty("GLOBALID"); short xfrAssetType = (short)xfrQryResults.Element[0].GetProperty("ASSETTYPE"); // Execute a downstream trace to find all customer low voltage service points IStringArray lvsGlobalIDs = FindLVServicePoints(xfrGlobalID); // Prepare response JSON JSONObject xfrInfo = new JSONObject(); xfrInfo.AddString("ASSETID", xfrAssetID); xfrInfo.AddString("GLOBALID", xfrGlobalID); xfrInfo.AddLong("ASSETGROUP", MV_XFR_ASSETGROUP); xfrInfo.AddLong("ASSETTYPE", xfrAssetType); IJSONObject xfrPointJson = new JSONObject(); IGeometry xfrPoint = (IGeometry)xfrQryResults.Element[0].GetProperty("SHAPE"); IJSONConverterGeometry geoSerializer = new JSONConverterGeometryClass(); geoSerializer.QueryJSONGeometry(xfrPoint, false, xfrPointJson); xfrInfo.AddJSONObject("geometry", xfrPointJson); result.AddJSONObject("transformer", xfrInfo); IJSONArray servicePoints = new JSONArrayClass(); // Get additional service point info if any returned from trace if (lvsGlobalIDs != null && lvsGlobalIDs.Count > 0) { // Query devices to get more details about service points string lvsGlobalIDList = ""; for (int i = 0; i < lvsGlobalIDs.Count; i++) { lvsGlobalIDList += (i == 0) ? "" : ","; lvsGlobalIDList += "'" + lvsGlobalIDs.Element[i] + "'"; } string lvsWhereClause = "GLOBALID IN (" + lvsGlobalIDList + ")"; IStringArray lvsOutFields = new StrArrayClass(); lvsOutFields.Add("GLOBALID"); lvsOutFields.Add("ASSETTYPE"); lvsOutFields.Add("ASSETGROUP"); lvsOutFields.Add("ASSETID"); lvsOutFields.Add("SHAPE"); IPropertySetArray lvsQryResults = QueryDevices(lvsWhereClause, lvsOutFields); // Get customer details if (lvsQryResults != null && lvsQryResults.Count > 0) { for (int i = 0; i < lvsQryResults.Count; i++) { IPropertySet lvs = lvsQryResults.Element[i]; JSONObject servicePoint = new JSONObject(); servicePoint.AddString("GLOBALID", (string)lvs.GetProperty("GLOBALID")); servicePoint.AddString("ASSETID", (string)lvs.GetProperty("ASSETID")); servicePoint.AddLong("ASSETGROUP", (int)lvs.GetProperty("ASSETGROUP")); servicePoint.AddLong("ASSETTYPE", (short)lvs.GetProperty("ASSETTYPE")); IGeometry lvsPoint = (IGeometry)lvs.GetProperty("SHAPE"); IJSONObject lvsPointJson = new JSONObject(); geoSerializer.QueryJSONGeometry(lvsPoint, false, lvsPointJson); servicePoint.AddJSONObject("geometry", lvsPointJson); JSONObject customerInfo = GetCustomerInfoJSON(); servicePoint.AddJSONObject("customerInfo", customerInfo); servicePoints.AddJSONObject(servicePoint); } } } result.AddJSONArray("servicePoints", servicePoints); } catch (Exception e) { logger.LogMessage(ServerLogger.msgType.debug, soe_name + " getCustomers", 500, "Exception " + e.GetType().Name + " " + e.Message + " " + e.StackTrace); JSONObject error = soeUtil.CreateErrorJSON(500, "Unable to complete operation", e.HResult, e.Message); return(Encoding.UTF8.GetBytes(error.ToJSONString(null))); } return(Encoding.UTF8.GetBytes(result.ToJSONString(null))); }
/** * Performs edits to the geodatabase powering the map service that this SOE * extends * * @param feature * @param featureJSON * @throws Exception */ private byte[] addFeature(JsonObject featureJSON, out IFeature feature) { feature = null; IDataset fsDataset = (IDataset)this.fc; IWorkspace ws = fsDataset.Workspace; IWorkspaceEdit wsEdit = (IWorkspaceEdit)ws; try { // start an edit transaction to add a new feature to feature class wsEdit.StartEditing(false); wsEdit.StartEditOperation(); feature = fc.CreateFeature(); // set attributes if (this.editLayerInfo == null) { this.editLayerInfo = this.layerInfos.get_Element(this.layerId); if (!this.editLayerInfo.IsFeatureLayer) { return createErrorObject( 403, "The layerId property of this SOE currently points to a layer (id: " + this.layerId + ") that is not a feature layer.", new String[] { "Only feature layers can be edited by this SOE.", "Modify SOE's layerId property using ArcGIS Manager or ArcGIS Desktop's Service Editor." }); } } IFields fields = this.editLayerInfo.Fields; JsonObject attributesJSON = null; featureJSON.TryGetJsonObject("attributes", out attributesJSON); System.Collections.IEnumerator itKeys = attributesJSON.GetEnumerator(); while (itKeys.MoveNext()) { KeyValuePair<string, object> kv = (KeyValuePair<string, object>)itKeys.Current; String key = kv.Key; int fieldId = fields.FindField(key); IField field = fields.get_Field(fieldId); object fieldValue = null; if (field.Editable) { //not using specific types based on field type, since can't assign value of any type to C# object attributesJSON.TryGetObject(key, out fieldValue); // set attribute field value feature.set_Value(fieldId, fieldValue); } } // retrieve geometry as json and convert it to ArcObject geometry JsonObject geometryJSON = null; featureJSON.TryGetJsonObject("geometry", out geometryJSON); IJSONConverterGeometry iConverter = new JSONConverterGeometryClass(); IJSONObject obj = new JSONObjectClass(); obj.ParseString(geometryJSON.ToJson()); IGeometry geometry = null; switch (this.fc.ShapeType) { case esriGeometryType.esriGeometryPoint: geometry = iConverter.ToPoint(obj); break; case esriGeometryType.esriGeometryMultipoint: geometry = iConverter.ToMultipoint(obj, false, false); break; case esriGeometryType.esriGeometryPolyline: geometry = iConverter.ToPolyline(obj, false, false); break; case esriGeometryType.esriGeometryPolygon: geometry = iConverter.ToPolygon(obj, false, false); break; } // set geometry feature.Shape = geometry; // store feature in feature class feature.Store(); // end edit transaction wsEdit.StopEditOperation(); wsEdit.StopEditing(true); } catch (Exception e) { if (wsEdit != null && wsEdit.IsBeingEdited()) { wsEdit.StopEditing(false); } return createErrorObject(500, "Error occured while editing layer " + this.layerId + ".", new String[] { "Error details:", e.Message }); } return Encoding.UTF8.GetBytes(System.Boolean.TrueString); }
/** * Performs edits to the geodatabase powering the map service that this SOE * extends * * @param feature * @param featureJSON * @throws Exception */ private byte[] addFeature(JsonObject featureJSON, out IFeature feature) { feature = null; IDataset fsDataset = (IDataset)this.fc; IWorkspace ws = fsDataset.Workspace; IWorkspaceEdit wsEdit = (IWorkspaceEdit)ws; try { // start an edit transaction to add a new feature to feature class wsEdit.StartEditing(false); wsEdit.StartEditOperation(); feature = fc.CreateFeature(); // set attributes if (this.editLayerInfo == null) { this.editLayerInfo = this.layerInfos.get_Element(this.layerId); if (!this.editLayerInfo.IsFeatureLayer) { return(createErrorObject( 403, "The layerId property of this SOE currently points to a layer (id: " + this.layerId + ") that is not a feature layer.", new String[] { "Only feature layers can be edited by this SOE.", "Modify SOE's layerId property using ArcGIS Manager or ArcGIS Desktop's Service Editor." })); } } IFields fields = this.editLayerInfo.Fields; JsonObject attributesJSON = null; featureJSON.TryGetJsonObject("attributes", out attributesJSON); System.Collections.IEnumerator itKeys = attributesJSON.GetEnumerator(); while (itKeys.MoveNext()) { KeyValuePair <string, object> kv = (KeyValuePair <string, object>)itKeys.Current; String key = kv.Key; int fieldId = fields.FindField(key); IField field = fields.get_Field(fieldId); object fieldValue = null; if (field.Editable) { //not using specific types based on field type, since can't assign value of any type to C# object attributesJSON.TryGetObject(key, out fieldValue); // set attribute field value feature.set_Value(fieldId, fieldValue); } } // retrieve geometry as json and convert it to ArcObject geometry JsonObject geometryJSON = null; featureJSON.TryGetJsonObject("geometry", out geometryJSON); IJSONConverterGeometry iConverter = new JSONConverterGeometryClass(); IJSONObject obj = new JSONObjectClass(); obj.ParseString(geometryJSON.ToJson()); IGeometry geometry = null; switch (this.fc.ShapeType) { case esriGeometryType.esriGeometryPoint: geometry = iConverter.ToPoint(obj); break; case esriGeometryType.esriGeometryMultipoint: geometry = iConverter.ToMultipoint(obj, false, false); break; case esriGeometryType.esriGeometryPolyline: geometry = iConverter.ToPolyline(obj, false, false); break; case esriGeometryType.esriGeometryPolygon: geometry = iConverter.ToPolygon(obj, false, false); break; } // set geometry feature.Shape = geometry; // store feature in feature class feature.Store(); // end edit transaction wsEdit.StopEditOperation(); wsEdit.StopEditing(true); } catch (Exception e) { if (wsEdit != null && wsEdit.IsBeingEdited()) { wsEdit.StopEditing(false); } feature = null; return(createErrorObject(500, "Error occured while editing layer " + this.layerId + ".", new String[] { "Error details:", e.Message })); } return(Encoding.UTF8.GetBytes(System.Boolean.TrueString)); }