private void btnCreatePoly_Click(object sender, EventArgs e) { MyPoly poly = new MyPoly(); MyPoint p0 = new MyPoint(-8167838.556196676, 5040265.098729953); poly.Ring.Add(p0); MyPoint p1 = new MyPoint(-8165392.571291549, 5036137.499202552); poly.Ring.Add(p1); MyPoint p2 = new MyPoint(-8166004.06751783, 5035678.877032841); poly.Ring.Add(p2); MyPoint p3 = new MyPoint(-8171354.659497795, 5035678.877032841); poly.Ring.Add(p3); MyPoint p4 = new MyPoint(-8167838.556196676, 5040265.098729953); poly.Ring.Add(p4); EditFeatureService(EditType.add, GeometryType.polygon, null, poly); }
/// <summary> /// This operation adds, updates and deletes features to the associated feature layer or table(POST only). /// </summary> /// <param name="type"></param> private void EditFeatureService(EditType type, GeometryType geomType, MyPoint myPoint, MyPoly myPoly) { string formattedRequest = string.Empty; string jsonResponse = string.Empty; string jsonToSend = string.Empty; string attributes = string.Empty; string geometry = string.Empty; string url = CompleteServiceUrl(); switch (type) { case EditType.add: { attributes = "\"attributes\":{\""; int counter = 0; if (_records != null) { foreach (IDataRecord record in _records) { foreach (string field in _fieldNames) { attributes += string.Format("{0}\":\"{1}\",\"", field, record[counter].ToString()); counter++; } break; } } if (counter == 0) //no records added. Create a dummy record. { foreach (DataGridViewRow row in dataGridViewFields.Rows) { //todo: did I set a default color symbol? attributes += string.Format("{0}\":\"{1}\",\"", row.Cells[0].FormattedValue.ToString(), null); } } //remove the excess. Meaning remove the trailing rubbish attributes = attributes.Remove(attributes.Length - 8); if (geomType == GeometryType.polygon) { //Polygon geometry MyPoint p; jsonToSend = "adds=[{\"geometry\":{\"rings\":[["; for (int i = 0; i < myPoly.Ring.Count; i++) { p = myPoly.Ring[i]; jsonToSend += "[" + p.X + "," + p.Y + "],"; } jsonToSend = jsonToSend.Remove(jsonToSend.Length - 1, 1); jsonToSend += "]],\"spatialReference\":{\"wkid\":102100}}," + attributes + "}}]"; //example // [{"geometry":{"rings":[[[-8304737.273855386,5018862.730810074],[-8286086.638953812,5017945.486470653],[-8280583.172917282,5006632.806284452], //[-8303820.029515964,4995931.622324532],[-8322164.916304397,5006938.554397592],[-8304737.273855386,5018862.730810074]]], //"spatialReference":{"wkid":102100}},"attributes":{"BUFF_DIST":"3","BufferArea":null,"BufferPerimeter":null}}] } else { //NB: Sample does not provide an entry method for the user to enter an XY for point geom types. //this is for demo purposes with a point feature service with the spatial ref as set below. //Users of this code need to build this functionality into their app, either with text input or map click. //Supplied XY places a point on the Coronado Bridge, San Diego, California jsonToSend = "adds=[{\"geometry\":{\"x\":" + myPoint.X + ",\"y\":" + myPoint.Y + ",\"spatialReference\":{\"wkid\":102100}}," + attributes + "}}]"; } break; } case EditType.delete: { break; } case EditType.update: { break; } default: break; } //Make the HttpWebRequest _featureEditResponse = RequestAndResponseHandler.FeatureEditRequest(url, jsonToSend, out jsonResponse); switch (type) { case EditType.add: { if (_featureEditResponse.addResults == null) break; lblEditingResponse.Text = string.Format("Success: {0}, ObjectID: {1}, GlobalID: {2}, Error: {3}", _featureEditResponse.addResults[0].success, _featureEditResponse.addResults[0].objectId, _featureEditResponse.addResults[0].globalId, _featureEditResponse.addResults[0].error); break; } case EditType.delete: { break; } case EditType.update: { break; } default: break; } }
private void DrawShapes(Graphics g) { try { string errMessage = ""; //ListXMLShapes.Clear(); XmlNodeList xml_lines, xml_arrows, xml_rects, xml_regions; XmlDocument xmlDoc = new XmlDocument(); RecognizerInfo ri = RecognizerBusiness.Instance.GetRecognizerInfoByCameraId(ref errMessage, _cameraID); if (ri == null) { //if (XtraMessageBox.Show("对不起,您使用的照片没有对应的识别器,请另选", "提示", MessageBoxButtons.YesNoCancel) == DialogResult.Yes) { return; } } string name = @"c:\" + ri.Id.ToString() + "." + _cameraID + "admin" + ".xml"; xmlDoc.Load(@name); //直线 xml_lines = xmlDoc.SelectSingleNode("/pr/cameras/camera/lines").ChildNodes; float xScale = (float)this.Width / _currentImage.Width; float yScale = (float)(this.Height) / _currentImage.Height; foreach (XmlNode lineitem in xml_lines) { MyLine line = new MyLine(); line.MyPen = new Pen(Color.Red, 1); XmlElement xe = (XmlElement)lineitem; line.P1.X = Convert.ToInt32(xe.GetAttribute("X1")); line.P1.X = (int)(line.P1.X * xScale); line.P1.Y = Convert.ToInt32(xe.GetAttribute("Y1")); line.P1.Y = (int)(line.P1.Y * yScale); line.P2.X = Convert.ToInt32(xe.GetAttribute("X2")); line.P2.X = (int)(line.P2.X * xScale); line.P2.Y = Convert.ToInt32(xe.GetAttribute("Y2")); line.P2.Y = (int)(line.P2.Y * yScale); line.MyPen.Color = ColorTranslator.FromHtml(xe.GetAttribute("PenColor")); line.MyPen.Width = Convert.ToInt32(xe.GetAttribute("PenWidth")); g.DrawLine(line.MyPen, line.P1, line.P2); //ListXMLShapes.Add(line); } //箭头 xml_arrows = xmlDoc.SelectSingleNode("/pr/cameras/camera/arrows").ChildNodes; foreach (XmlNode arrowitem in xml_arrows) { MyArrow arrow = new MyArrow(); arrow.MyPen = new Pen(Color.Red, 1); XmlElement xa = (XmlElement)arrowitem; arrow.P1.X = Convert.ToInt32(xa.GetAttribute("X1")); arrow.P1.X = (int)(arrow.P1.X * xScale); arrow.P1.Y = Convert.ToInt32(xa.GetAttribute("Y1")); arrow.P1.Y = (int)(arrow.P1.Y * yScale); arrow.P2.X = Convert.ToInt32(xa.GetAttribute("X2")); arrow.P2.X = (int)(arrow.P2.X * xScale); arrow.P2.Y = Convert.ToInt32(xa.GetAttribute("Y2")); arrow.P2.Y = (int)(arrow.P2.Y * yScale); arrow.MyPen.Color = ColorTranslator.FromHtml(xa.GetAttribute("PenColor")); arrow.MyPen.Width = Convert.ToInt32(xa.GetAttribute("PenWidth")); g.DrawLine(arrow.MyPen, arrow.P1, arrow.P2); //ListXMLShapes.Add(arrow); } //矩形 xml_rects = xmlDoc.SelectSingleNode("/pr/cameras/camera/rects").ChildNodes; foreach (XmlNode rectitem in xml_rects) { MyRect rect = new MyRect(); rect.MyPen = new Pen(Color.Red, 1); XmlElement xr = (XmlElement)rectitem; rect.P1.X = Convert.ToInt32(xr.GetAttribute("X")); rect.P1.X = (int)(rect.P1.X * xScale); rect.P1.Y = Convert.ToInt32(xr.GetAttribute("Y")); rect.P1.Y = (int)(rect.P1.Y * yScale); rect.Width = Convert.ToInt32(xr.GetAttribute("W")); rect.Width = (int)(rect.Width * xScale); rect.Height = Convert.ToInt32(xr.GetAttribute("H")); rect.Height = (int)(rect.Height * yScale); rect.MyPen.Color = ColorTranslator.FromHtml(xr.GetAttribute("PenColor")); rect.MyPen.Width = Convert.ToInt32(xr.GetAttribute("PenWidth")); g.DrawRectangle(rect.MyPen, rect.P1.X, rect.P1.Y, rect.Width, rect.Height); //ListXMLShapes.Add(rect); } //多边形 xml_regions = xmlDoc.SelectSingleNode("/pr/cameras/camera/regions").ChildNodes; foreach (XmlNode regionitem in xml_regions) { MyPoly poly = new MyPoly(); poly.MyPen = new Pen(Color.Red, 1); XmlElement xp = (XmlElement)regionitem; poly.MyPen.Color = ColorTranslator.FromHtml(xp.GetAttribute("PenColor")); poly.MyPen.Width = Convert.ToInt32(xp.GetAttribute("PenWidth")); XmlNodeList pointlist = regionitem.ChildNodes; foreach (XmlNode pitem in pointlist) { Point p = new Point(); XmlElement test = (XmlElement)pitem; p.X = Convert.ToInt32(test.GetAttribute("X")); p.X = (int)(p.X * xScale); p.Y = Convert.ToInt32(test.GetAttribute("Y")); p.Y = (int)(p.Y * yScale); poly.ListPoint.Add(p); } //IsFinished=true poly.IsFinished = true; g.DrawPolygon(poly.MyPen, poly.ListPoint.ToArray()); //ListXMLShapes.Add(poly); } } catch (Exception) { ; } }