예제 #1
0
 private void Button_Load(object sender, System.Windows.RoutedEventArgs e)
 {
     try
     {
         FeatureLayer featureLayer = FeatureLayer.FromJson(JsonTextBox.Text);
         featureLayer.RendererTakesPrecedence = false;
         MyMap.Layers.Add(featureLayer);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "FeatureLayer creation failed", MessageBoxButton.OK);
     }
 }
예제 #2
0
 private void CreateFeatureLayerButton_Click(object sender, EventArgs e)
 {
     try
     {
         FeatureLayer featureLayer = FeatureLayer.FromJson(jsonInput);
         featureLayer.RendererTakesPrecedence = false;
         MyMap.Layers.Add(featureLayer);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "FeatureLayer creation failed", MessageBoxButton.OK);
     }
 }
 private void processLayerInfoResult(ArcGISWebClient.DownloadStringCompletedEventArgs e)
 {
     #region Parse layer info from json
     if (e.Cancelled)
     {
         return;
     }
     if (e.Error != null)
     {
         singleLayerRequestCompleted(null, e);
         return;
     }
     string json = null;
     try
     {
         json = e.Result;
     }
     catch (Exception exception)
     {
         if (exception != null)
         {
             singleLayerRequestCompleted(null, e);
             return;
         }
     }
     Exception ex = ESRI.ArcGIS.Mapping.DataSources.Utils.CheckJsonForException(json);
     if (ex != null)
     {
         singleLayerRequestCompleted(null, e);
         return;
     }
     json = "{\"layerDefinition\":" + json + "}";
     FeatureLayer     featureLayer     = FeatureLayer.FromJson(json);
     FeatureLayerInfo featurelayerinfo = featureLayer.LayerInfo;
     if (featurelayerinfo == null)
     {
         singleLayerRequestCompleted(null, e);
         return;
     }
     LayerInformation info = new LayerInformation()
     {
         ID            = featurelayerinfo.Id,
         DisplayField  = featurelayerinfo.DisplayField,
         Name          = featurelayerinfo.Name,
         PopUpsEnabled = false,
         LayerJson     = json,
         FeatureLayer  = featureLayer
     };
     Collection <ESRI.ArcGIS.Mapping.Core.FieldInfo> fieldInfos = new Collection <ESRI.ArcGIS.Mapping.Core.FieldInfo>();
     if (featurelayerinfo.Fields != null)
     {
         foreach (ESRI.ArcGIS.Client.Field field in featurelayerinfo.Fields)
         {
             if (FieldHelper.IsFieldFilteredOut(field.Type))
             {
                 continue;
             }
             ESRI.ArcGIS.Mapping.Core.FieldInfo fieldInfo = ESRI.ArcGIS.Mapping.Core.FieldInfo.FieldInfoFromField(featureLayer, field);
             fieldInfos.Add(fieldInfo);
         }
     }
     info.Fields = fieldInfos;
     if (fieldInfos.Count > 0)
     {
         singleLayerRequestCompleted(info, e);
     }
     else
     {
         singleLayerRequestCompleted(null, e);
     }
     #endregion
 }
        private void BrowseButton_Click(object sender, RoutedEventArgs e)
        {
            // Call the ShowDialog method to show the dialog box.
            bool?userClickedOK = openFileDialog.ShowDialog();

            if (userClickedOK == true)
            {
                // Open the selected file to read.
                System.IO.FileInfo file = openFileDialog.File;

                // Uri to the ArcGIS Portal API generate operation.
                // Reference documentation available here: http://www.arcgis.com/apidocs/rest/generate.html
                Uri address = new Uri("http://www.arcgis.com/sharing/rest/content/features/generate");

                // Get the file contents for the local file
                FileStream fs = file.OpenRead();

                // Create ArcGISWebClient.StreamContent instance
                ArcGISWebClient.StreamContent streamContent = new ArcGISWebClient.StreamContent()
                {
                    Name        = "file",
                    Filename    = file.Name,
                    Stream      = fs,
                    ContentType = "application/zip"
                };

                // Create a list of stream content to POST
                IList <ArcGISWebClient.StreamContent> filestream = new List <ArcGISWebClient.StreamContent>();
                filestream.Add(streamContent);

                // Create dictionary to store parameter to POST
                Dictionary <string, string> postParameters = new Dictionary <string, string>();

                // A class created to store publish parameters for the generate operation
                GenerateFeaturesParams param = new GenerateFeaturesParams()
                {
                    name            = file.Name.Substring(0, file.Name.LastIndexOf(".")),
                    maxRecordCount  = 1000,
                    generalize      = false,
                    reducePrecision = true,
                    targetSR        = MyMap.SpatialReference
                };

                // Must specify the output type (json) the file type (shapefile) and the publish parameters
                postParameters.Add("f", "json");
                postParameters.Add("filetype", "shapefile");
                postParameters.Add("publishParameters", SerializeToJsonString(param));

                // Url to the generate operation, part of the ArcGIS Portal REST API (http://www.arcgis.com/apidocs/rest/generate.html)
                string postURL = "http://www.arcgis.com/sharing/rest/content/features/generate";

                // Use ArcGISWebClient POST shapefile to the ArcGIS Portal generate operation.  The generate operation requires a file to be passed
                // in a multi-part post request.
                ArcGISWebClient agsWebClient = new ArcGISWebClient();
                agsWebClient.PostMultipartCompleted += (a, b) =>
                {
                    if (b.Error == null)
                    {
                        try
                        {
                            // Use the the generic JsonValue to handle dynamic json content.
                            // In this case, generate always returns a "featureCollection" object which contains
                            // a "layers" array with one feature layer.
                            JsonValue featureCollection = JsonValue.Load(b.Result);
                            string    layer             = featureCollection["featureCollection"]["layers"][0].ToString();

                            FeatureLayer featureLayer = FeatureLayer.FromJson(layer);

                            if (featureLayer != null)
                            {
                                // Add the feature layer to the map and zoom to it
                                MyMap.Layers.Add(featureLayer);
                                MyMap.ZoomTo(featureLayer.FullExtent.Expand(1.25));
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "FeatureLayer creation failed", MessageBoxButton.OK);
                        }
                    }
                };
                agsWebClient.PostMultipartAsync(new Uri(postURL), postParameters, filestream, null);
            }
        }