Пример #1
0
        /// <summary>
        /// Function provides you with two separate work flows based on what happens in the UI. If a connection to 
        /// a SQL server table, the table structure will have been copied into the member variable _fieldNameTypeDict.
        /// If the user has described the field names and field types, we use that instead.
        /// </summary>
        /// <param name="fields"></param>
        private void Fields(List<object> fields)
        {
            //DO NOT CHANGE PROPERTIES FOR THE fieldFID
              Field fieldFID = new Field()
              {
            name = "OBJECTID",
            type = "esriFieldTypeInteger",
            alias = "OBJECTID",
            sqlType = "sqlTypeInteger",
            nullable = false,
            editable = false,
            domain = null,
            defaultValue = null
              };

              fields.Add(fieldFID);

              //The following loops through the dictionary of value pairs required for the feature service attribution
              //table. Using the field type, create the appropriate field type and use the key value as the name of the
              //field. I have listed only field field types, please expand on this for your needs. Listed at the top of
              //this field are the ESRI field types you can use.
              if (_sqlServerTableFieldNameAndTypeDictionary != null)
              {
            foreach (KeyValuePair<string, string> keyValuePair in _sqlServerTableFieldNameAndTypeDictionary)
            {
              Console.WriteLine("Key = {0}, Value = {1}", keyValuePair.Key, keyValuePair.Value);

              if (keyValuePair.Value == "nvarchar")
              {
            FieldString field0 = new FieldString()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeString",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field0);
              }

              if (keyValuePair.Value == "float")
              {
            Field field1 = new Field()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeDouble",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeFloat",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field1);
              }

              if (keyValuePair.Value == "int")
              {
            Field fieldInt = new Field()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeInteger",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeInteger",
              nullable = false,
              editable = false,
              domain = null,
              defaultValue = 0
            };

            fields.Add(fieldInt);
              }

              if (keyValuePair.Value == "char")
              {
            FieldString fieldchar = new FieldString()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeString",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(fieldchar);
              }

              if (keyValuePair.Value == "datetime")
              {
            FieldString fieldchar = new FieldString()
            {
              name = keyValuePair.Key,
              type = "esriFieldTypeDate",
              alias = keyValuePair.Key,
              sqlType = "sqlTypeDateTime",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(fieldchar);
              }
            }
              }
              else
              {
            foreach (DataGridViewRow row in dataGridViewFields.Rows)
            {
              if (row.Cells[1].FormattedValue.ToString() == "Text")
              {
            FieldString field0 = new FieldString()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeString",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field0);
              }

              if (row.Cells[1].FormattedValue.ToString() == "Float" || row.Cells[1].FormattedValue.ToString() == "Double")
              {
            Field field1 = new Field()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeDouble",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeFloat",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field1);
              }

              if (row.Cells[1].FormattedValue.ToString() == "Raster")
              {
            Field field1 = new Field()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeRaster",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeFloat", //??? Todo
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(field1);
              }

              if (row.Cells[1].FormattedValue.ToString() == "Short" || row.Cells[1].FormattedValue.ToString() == "Long")
              {
            Field fieldInt = new Field()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeInteger",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeInteger",
              nullable = false,
              editable = false,
              domain = null,
              defaultValue = 0
            };

            fields.Add(fieldInt);
              }

              if (row.Cells[1].FormattedValue.ToString() == "Date")
              {
            FieldString fieldchar = new FieldString()
            {
              name = row.Cells[0].FormattedValue.ToString(),
              type = "esriFieldTypeDate",
              alias = row.Cells[0].FormattedValue.ToString(),
              sqlType = "sqlTypeDateTime",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            fields.Add(fieldchar);
              }
            }
              }
        }
Пример #2
0
        /// <summary>
        /// NB: This code creates a point feature service if nothing has been selected in the "My Feature Services Info -> Items combobox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddDefinitionToLayer_Click(object sender, EventArgs e)
        {
            Item item = null;
              Extent extent = null;
              Symbol symbol = null;
              Renderer renderer = null;
              DrawingInfo drawingInfo = null;
              object[] fields = null;
              Template template = null;
              EditorTrackingInfo editorTrackingInfo = null;
              AdminLayerInfoAttribute adminLayerInfo = null;
              DefinitionLayer layer = null;
              string formattedRequest = string.Empty;
              string jsonResponse = string.Empty;

              FeatureLayerAttributes featLayerAttributes = null;

              this.Cursor = Cursors.WaitCursor;

              //NB: From the Items combobox if you have a feature service selected, this is the attribute table structure that will be used for the
              //creation of the new service. Otherwise create a new featureLayerAttributes class as set up in this code.
              //
              if (chkbxUseSelectedFS.Checked)
              {
            string[] concatenatedText = cboItems.Text.Split(':');
            string split = concatenatedText[1].Replace(" ID", "");
            if (_myOrganizationalContent.TryGetValue(cboItems.Text, out item))
              if(item != null)
            if(item.url != null)
              featLayerAttributes = RequestAndResponseHandler.GetFeatureServiceAttributes(item.url, _token, out formattedRequest, out jsonResponse);

            try
            {
              if (featLayerAttributes == null)
            _featureServiceAttributesDataDictionary.TryGetValue(split.Trim(), out _featureLayerAttributes);
            }
            catch { }
              }

              if (featLayerAttributes == null)
            if (_featureLayerAttributes != null)
              featLayerAttributes = _featureLayerAttributes;
            else
              featLayerAttributes = new FeatureLayerAttributes();

              //ensure that we have all that we need for a successful feature layer attributes push
              //
              if(featLayerAttributes.extent != null)
            extent = featLayerAttributes.extent;
              else
              {
            //write in your default extent values here:
            extent = new Extent()
            {
              xmin = -14999999.999999743,
              ymin = 1859754.5323447795,
              xmax = -6199999.999999896,
              ymax = 7841397.327701188,
              spatialReference = new SpatialReference() { wkid = 102100,  latestWkid = 3857 },
            };
              }

              if (featLayerAttributes.drawingInfo != null)
            drawingInfo = featLayerAttributes.drawingInfo;
              else
              {
            symbol = new PointSymbol()
            {
              type = "esriPMS",
              url = "RedSphere.png",
              imageData = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQBQYWludC5ORVQgdjMuNS4xTuc4+QAAB3VJREFUeF7tmPlTlEcexnve94U5mANQbgQSbgiHXHINlxpRIBpRI6wHorLERUmIisKCQWM8cqigESVQS1Kx1piNi4mW2YpbcZONrilE140RCTcy3DDAcL/zbJP8CYPDL+9Ufau7uqb7eZ7P+/a8PS8hwkcgIBAQCAgEBAICAYGAQEAgIBAQCAgEBAICAYGAQEAgIBAQCDx/AoowKXFMUhD3lQrioZaQRVRS+fxl51eBTZUTdZ41U1Rox13/0JF9csGJ05Qv4jSz/YPWohtvLmSKN5iTGGqTm1+rc6weICOBRbZs1UVnrv87T1PUeovxyNsUP9P6n5cpHtCxu24cbrmwKLdj+osWiqrVKhI0xzbmZ7m1SpJ+1pFpvE2DPvGTomOxAoNLLKGLscZYvB10cbYYjrJCb7A5mrxleOBqim+cWJRakZY0JfnD/LieI9V1MrKtwokbrAtU4Vm0A3TJnphJD4B+RxD0u0LA7w7FTE4oprOCMbklEGNrfdGf4IqnQTb4wc0MFTYibZqM7JgjO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6Bnfyg6V7ugz5yBhuHwrZjBdiU5YJg7I8wOpifAKoVIW7uQ3rpOBH2b3ekVjYT2WCRG3o+mIGKgO0OrlIaebU/HYOQDNbQnojB4NJyGD0NPfjA0bwTRE6Q7hsUcWhkWN8yZqSQlWWGECAZLmJfJmbrvVSI8taK37xpbdB/wQW8xPee/8xIGjvlj8IQ/hk4G0JbWcX8MHPVDX4kveoq8ocn3xLM33NCZRcPHOGJYZIKfpQyq7JjHS6yJjcHujLHADgkpuC7h8F8zEVqXSNC2awE69lqhs8AamkO26HrbDt2H7dBVQov2NcW26CiwQtu+BWjdY4n2nZboTbfCmKcCnRyDO/YmyLPnDlHvjDH8G6zhS9/wlEnYR7X00fWrFYuWdVI0ZpuhcbcczW/R2qdAcz6t/bRov4mONeaaoYl+p22rHF0bVNAmKtBvweIXGxNcfFH8eNlC4m6wMWMusEnKpn5hyo48pj9gLe4SNG9QoGGLAk8z5XiaJUd99u8122/IpBA2K9BGg2vWWKAvRYVeLzEa7E1R422m2+MsSTem97nSYnfKyN6/mzATv7AUgqcMrUnmaFlLX3ysM0fj+t/b5lQLtK22QEfyAmiSLKFZpUJ7kBRPXKW4HqCYynWVHKSG2LkyZex1uO1mZM9lKem9Tx9jjY5iNEYo0bKMhn7ZAu0r6H5PpLXCAq0rKJClSjSGynE/QIkrQYqBPe6S2X+AJsY2Ped6iWZk6RlL0c2r5szofRsO9R5S1IfQLRCpQL1aifoYFerpsbkuTImaUJXuXIDiH6/Ys8vm3Mg8L2i20YqsO7fItKLcSXyn0kXccclVqv3MS6at9JU/Ox+ouns+SF6Z4cSupz7l8+z1ucs7LF1AQjOdxfGZzmx8Iu1TRcfnrioICAQEAgIBgYBAQCAgEBAICAQEAgIBgYBAQCAgEBAICAQEAv8H44b/6ZiGvGAAAAAASUVORK5CYII=",
              contentType = "image/png",
              color = null,
              width = 15,
              height = 15,
              angle = 0,
              xoffset = 0,
              yoffset = 0
            };

            renderer = new PointRenderer()
            {
              type = "simple",
              symbol = symbol,
              label = "",
              description = ""
            };

            drawingInfo = new DrawingInfo()
            {
              renderer = renderer,
              labelingInfo = null
            };
              }

              if (featLayerAttributes.fields != null)
            fields = featLayerAttributes.fields;
              else
              {
            Field field = new Field()
            {
              name = "Longitude",
              type = "esriFieldTypeDouble",
              alias = "Longitude",
              sqlType = "sqlTypeFloat",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            Field field2 = new Field()
            {
              name = "Latitude",
              type = "esriFieldTypeDouble",
              alias = "Latitude",
              sqlType = "sqlTypeFloat",
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            FieldString field3 = new FieldString()
            {
              name = "Name",
              type = "esriFieldTypeString",
              alias = "Name",
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            FieldString field4 = new FieldString()
            {
              name = "Address",
              type = "esriFieldTypeString",
              alias = "Address",
              sqlType = "sqlTypeNVarchar",
              length = 256,
              nullable = true,
              editable = true,
              domain = null,
              defaultValue = null
            };

            //DO NOT CHANGE PROPERTIES BELOW
            Field fieldFID = new Field()
            {
              name = "FID",
              type = "esriFieldTypeInteger",
              alias = "FID",
              sqlType = "sqlTypeInteger",
              nullable = false,
              editable = false,
              domain = null,
              defaultValue = null
            };

            //object array so that we can contain different types within.
            //Field type double for example does not contain a length parameter. Hence we need different field type
            //representation. Unexpected properties for data types will cause a failure on the server end.
            fields = new object[5] { field, field2, field3, field4, fieldFID };
              }

              if (featLayerAttributes.templates != null)
               template = featLayerAttributes.templates[0];
              else
              {
            template = new Template()
            {
              name = "New Feature",
              description = "",
              drawingTool = "esriFeatureEditToolPoint",
              prototype = new Prototype()
              {
            attributes = new Attributes()
              }
            };
              }

              editorTrackingInfo = new EditorTrackingInfo()
              {
            enableEditorTracking = false,
            enableOwnershipAccessControl = false,
            allowOthersToUpdate = true,
            allowOthersToDelete = true
              };

              adminLayerInfo = new AdminLayerInfoAttribute()
              {
            geometryField = new GeometryField()
            {
              name = "Shape",
              srid = 102100
            }
              };

              layer = new DefinitionLayer()
              {
            currentVersion  = featLayerAttributes != null ? featLayerAttributes.currentVersion : 10.11,
            id = 0,
            name = featLayerAttributes != null ? featLayerAttributes.name != null ? featLayerAttributes.name : txtFeatureServiceName.Text : txtFeatureServiceName.Text,
            type = featLayerAttributes != null ? featLayerAttributes.type != null ? featLayerAttributes.type : "Feature Layer" : "Feature Layer",
            displayField  = featLayerAttributes != null ? featLayerAttributes.displayField != null ? featLayerAttributes.displayField : "" : "",
            description = "",
            copyrightText  = featLayerAttributes != null ? featLayerAttributes.copyrightText != null ? featLayerAttributes.copyrightText : "" : "",
            defaultVisibility  = featLayerAttributes != null ? featLayerAttributes.defaultVisibility != null ? featLayerAttributes.defaultVisibility : true : true,
            relationships  = featLayerAttributes != null ? featLayerAttributes.relationShips != null ? featLayerAttributes.relationShips : new object[]{} : new object[] { },
            isDataVersioned  = featLayerAttributes != null ? featLayerAttributes.isDataVersioned : false,
            supportsRollbackOnFailureParameter = true,
            supportsAdvancedQueries = true,
            geometryType = featLayerAttributes != null ? featLayerAttributes.geometryType != null ? featLayerAttributes.geometryType : "esriGeometryPoint" : "esriGeometryPoint",
            minScale = featLayerAttributes != null ? featLayerAttributes.minScale : 0,
            maxScale  = featLayerAttributes != null ? featLayerAttributes.maxScale : 0,
            extent = extent,
            drawingInfo = _javaScriptSerializer.Serialize(drawingInfo),
            allowGeometryUpdates  = featLayerAttributes != null ? featLayerAttributes.allowGeometryUpdates != null ? featLayerAttributes.allowGeometryUpdates : true : true,
            hasAttachments  = featLayerAttributes != null ? featLayerAttributes.hasAttachments : false,
            htmlPopupType  = featLayerAttributes != null ? featLayerAttributes.htmlPopupType != null ? featLayerAttributes.htmlPopupType : "esriServerHTMLPopupTypeNone" : "esriServerHTMLPopupTypeNone",
            hasM  = featLayerAttributes != null ? featLayerAttributes.hasM : false,
            hasZ  = featLayerAttributes != null ? featLayerAttributes.hasZ : false,
            objectIdField  = featLayerAttributes != null ? featLayerAttributes.objectIdField != null ? featLayerAttributes.objectIdField : "FID" : "FID",
            globalIdField  = featLayerAttributes != null ? featLayerAttributes.globalIdField != null ? featLayerAttributes.globalIdField : "" : "",
            typeIdField = featLayerAttributes != null ? featLayerAttributes.typeIdField != null ? featLayerAttributes.typeIdField : "" : "",
            fields = fields,
            types = featLayerAttributes != null ? featLayerAttributes.types != null ? featLayerAttributes.types : new object[0] : new object[0],
            templates = new Template[1] { template },
            supportedQueryFormats  = featLayerAttributes != null ? featLayerAttributes.supportedQueryFormats != null ? featLayerAttributes.supportedQueryFormats: "JSON" : "JSON",
            hasStaticData  = featLayerAttributes != null ? featLayerAttributes.hasStaticData : false,
            maxRecordCount  = 2000,//featLayerAttributes != null ? featLayerAttributes.maxRecordCount != null ? featLayerAttributes.maxRecordCount : 200000 : 200000,
            capabilities = featLayerAttributes != null ? featLayerAttributes.capabilities != null ? featLayerAttributes.capabilities : "Query,Editing,Create,Update,Delete" : "Query,Editing,Create,Update,Delete",
            //editorTrackingInfo = editorTrackingInfo,
            adminLayerInfo = adminLayerInfo
              };

              DefinitionLayer[] layers = new DefinitionLayer[1] { layer };

              AddDefinition definition = new AddDefinition()
              {
            layers = layers
              };

              string serviceEndPoint = "http://services1.arcgis.com/"; //NB: Trial Account endpoint!!!!
              string serviceEndPoint2 = "http://services.arcgis.com/";

              string requestURL = string.Format("{0}{1}/arcgis/admin/services/{2}.FeatureServer/AddToDefinition", serviceEndPoint, _organizationID, _featureServiceCreationResponse.Name);

              bool b = RequestAndResponseHandler.AddToFeatureServiceDefinition(requestURL, definition, _token, txtOrgURL.Text, out formattedRequest, out jsonResponse);

              if (!b)
              {
            requestURL = string.Format("{0}{1}/arcgis/admin/services/{2}.FeatureServer/AddToDefinition", serviceEndPoint2, _organizationID, _featureServiceCreationResponse.Name);
            b = RequestAndResponseHandler.AddToFeatureServiceDefinition(requestURL, definition, _token, txtOrgURL.Text, out formattedRequest, out jsonResponse);
              }

              ShowRequestResponseStrings(formattedRequest, jsonResponse);

              lblSuccess.Text = b == true ? "true" : "false";

              this.Cursor = Cursors.Default;
        }