private void Button_Load(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                FeatureSet featureSet = FeatureSet.FromJson(JsonTextBox.Text);

                GraphicsLayer graphicsLayerFromFeatureSet = new GraphicsLayer()
                {
                    Graphics = new GraphicCollection(featureSet)
                };

                if (!featureSet.SpatialReference.Equals(MyMap.SpatialReference))
                {
                    if (MyMap.SpatialReference.Equals(new SpatialReference(102100)) &&
                        featureSet.SpatialReference.Equals(new SpatialReference(4326)))
                    {
                        foreach (Graphic g in graphicsLayerFromFeatureSet.Graphics)
                        {
                            g.Geometry = _mercator.FromGeographic(g.Geometry);
                        }
                    }

                    else if (MyMap.SpatialReference.Equals(new SpatialReference(4326)) &&
                             featureSet.SpatialReference.Equals(new SpatialReference(102100)))
                    {
                        foreach (Graphic g in graphicsLayerFromFeatureSet.Graphics)
                        {
                            g.Geometry = _mercator.ToGeographic(g.Geometry);
                        }
                    }

                    else
                    {
                        GeometryService geometryService =
                            new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

                        geometryService.ProjectCompleted += (s, a) =>
                        {
                            for (int i = 0; i < a.Results.Count; i++)
                            {
                                graphicsLayerFromFeatureSet.Graphics[i].Geometry = a.Results[i].Geometry;
                            }
                        };

                        geometryService.Failed += (s, a) =>
                        {
                            MessageBox.Show("Projection error: " + a.Error.Message);
                        };

                        geometryService.ProjectAsync(graphicsLayerFromFeatureSet.Graphics, MyMap.SpatialReference);
                    }
                }

                SimpleRenderer  simpleRenderer = new SimpleRenderer();
                SolidColorBrush symbolColor    = new SolidColorBrush(Colors.Blue);
                graphicsLayerFromFeatureSet.Renderer = simpleRenderer;

                if (featureSet.GeometryType == GeometryType.Polygon || featureSet.GeometryType == GeometryType.Polygon)
                {
                    simpleRenderer.Symbol = new SimpleFillSymbol()
                    {
                        Fill = symbolColor
                    };
                }
                else if (featureSet.GeometryType == GeometryType.Polyline)
                {
                    simpleRenderer.Symbol = new SimpleLineSymbol()
                    {
                        Color = symbolColor
                    };
                }
                else // Point
                {
                    simpleRenderer.Symbol = new SimpleMarkerSymbol()
                    {
                        Color = symbolColor,
                        Size  = 12
                    };
                }

                myInfoWindow = new InfoWindow()
                {
                    Background      = new SolidColorBrush(Colors.Black),
                    BorderBrush     = new SolidColorBrush(Colors.White),
                    BorderThickness = new Thickness(1),
                    CornerRadius    = 10,
                    Map             = MyMap,
                };
                myInfoWindow.MouseLeftButtonDown += myInfoWindow_MouseLeftButtonDown;

                StackPanel stackPanelParent = new StackPanel()
                {
                    Orientation = System.Windows.Controls.Orientation.Vertical,
                    Margin      = new Thickness(12)
                };

                foreach (KeyValuePair <string, string> keyvalue in featureSet.FieldAliases)
                {
                    StackPanel stackPanelChild = new StackPanel()
                    {
                        Orientation = System.Windows.Controls.Orientation.Horizontal,
                        Margin      = new Thickness(0, 0, 0, 6)
                    };
                    TextBlock textValue = new TextBlock()
                    {
                        Text = keyvalue.Value + ": "
                    };

                    TextBlock textKey    = new TextBlock();
                    Binding   keyBinding = new Binding(string.Format("[{0}]", keyvalue.Key));
                    textKey.SetBinding(TextBlock.TextProperty, keyBinding);

                    stackPanelChild.Children.Add(textValue);
                    stackPanelChild.Children.Add(textKey);

                    if (keyvalue.Key == featureSet.DisplayFieldName)
                    {
                        textKey.FontWeight = textValue.FontWeight = FontWeights.Bold;
                        stackPanelParent.Children.Insert(0, stackPanelChild);
                    }
                    else
                    {
                        stackPanelParent.Children.Add(stackPanelChild);
                    }
                }

                myInfoWindow.Content = stackPanelParent;
                ContentPanel.Children.Add(myInfoWindow);

                MyMap.Layers.Add(graphicsLayerFromFeatureSet);
                graphicsLayerFromFeatureSet.MouseLeftButtonDown += graphicsLayerFromFeatureSet_MouseLeftButtonDown;
                JsonPivot.Visibility = Visibility.Collapsed;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "GraphicsLayer creation failed", MessageBoxButton.OK);
            }
        }
Exemplo n.º 2
0
        }         // public void userGiveCSVLayerParams(string url, string lyrname, string proxy)

        /// <summary>
        /// Call from form, add to map graphicslayer from json
        /// </summary>
        /// <param name="url"></param>
        /// <param name="lyrname"></param>
        /// <param name="proxy"></param>
        public void userGiveJSONLayerParams(string url, string lyrname, string proxy)
        {
            log(string.Format("userGiveJSONLayerParams, name '{2}', url '{0}', proxy '{1}'", url, proxy, lyrname));
            MapApplication.Current.HideWindow(jsonParamsForm);

            var requrl = string.IsNullOrEmpty(proxy) ? url : string.Format("{0}?{1}", proxy, url);

            // get json text
            WebClient wc  = new WebClient();
            Uri       uri = new Uri(requrl, UriKind.RelativeOrAbsolute);

            wc.OpenReadCompleted += (sender, args) => {
                if (args.Error != null)
                {
                    log(String.Format("userGiveJSONLayerParams wc_OpenReadCompleted, error in reading answer, msg [{0}], trace [{1}]",
                                      args.Error.Message, args.Error.StackTrace));
                }
                else
                {
                    try {
                        StreamReader reader = new StreamReader(args.Result);
                        string       text   = reader.ReadToEnd();
                        log(string.Format("userGiveJSONLayerParams wc_OpenReadCompleted, resp '{0}'", text));

                        // got layer content, make layer
                        var featureSet    = FeatureSet.FromJson(text);
                        var graphicsLayer = new GraphicsLayer()
                        {
                            Graphics = new GraphicCollection(featureSet)
                        };
                        // set layer params
                        graphicsLayer.Opacity      = 1;
                        graphicsLayer.ID           = VExtClass.computeSHA1Hash(string.Format("{0}", text));
                        graphicsLayer.Initialized += (osender, eargs) => {
                            log(string.Format("userGiveJSONLayerParams.Initialized, {0}-{1}", lyrname, graphicsLayer.ID));
                        };

                        // projection
                        var MyMap    = MapApplication.Current.Map;
                        var mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();
                        if (!featureSet.SpatialReference.Equals(MyMap.SpatialReference))
                        {
                            if (MyMap.SpatialReference.Equals(new SpatialReference(102100)) &&
                                featureSet.SpatialReference.Equals(new SpatialReference(4326)))
                            {
                                foreach (Graphic g in graphicsLayer.Graphics)
                                {
                                    g.Geometry = mercator.FromGeographic(g.Geometry);
                                }
                            }

                            else if (MyMap.SpatialReference.Equals(new SpatialReference(4326)) &&
                                     featureSet.SpatialReference.Equals(new SpatialReference(102100)))
                            {
                                foreach (Graphic g in graphicsLayer.Graphics)
                                {
                                    g.Geometry = mercator.ToGeographic(g.Geometry);
                                }
                            }

                            else
                            {
                                var geometryService = new GeometryService(
                                    "http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

                                geometryService.ProjectCompleted += (s, a) => {
                                    for (int i = 0; i < a.Results.Count; i++)
                                    {
                                        graphicsLayer.Graphics[i].Geometry = a.Results[i].Geometry;
                                    }
                                };

                                geometryService.Failed += (s, a) => {
                                    MessageBox.Show("Ошибка проецирования: " + a.Error.Message);
                                };

                                geometryService.ProjectAsync(graphicsLayer.Graphics, MyMap.SpatialReference);
                            }
                        }                         // if map.SR != featureset.SR

                        // add layer to map
                        graphicsLayer.Initialize();
                        MapApplication.SetLayerName(graphicsLayer, lyrname);
                        ESRI.ArcGIS.Client.Extensibility.LayerProperties.SetIsVisibleInMapContents(graphicsLayer, true);
                        MapApplication.Current.Map.Layers.Add(graphicsLayer);
                    }                     // got json text
                    catch (Exception ex) {
                        log(String.Format("userGiveJSONLayerParams wc_OpenReadCompleted, make layer failed {0}, {1}", ex.Message, ex.StackTrace));
                    }
                }
            };             // wc.OpenReadCompleted
            wc.OpenReadAsync(uri);

            log(string.Format("userGiveJSONLayerParams, done for name '{2}', url '{0}', proxy '{1}'", url, proxy, lyrname));
        }         // public void userGiveJSONLayerParams(string url, string lyrname, string proxy)