Exemple #1
0
        private async Task CalculateViewshed(MapPoint location)
        {
            // Create new geoprocessing task
            var viewshedTask = new GeoprocessingTask(new Uri(ViewshedUrl));

            // Create input collection that contains the requested location
            var     inputFeatures = new FeatureCollectionTable(new List <Field>(), GeometryType.Point, MyMapView.SpatialReference);
            Feature inputFeature  = inputFeatures.CreateFeature();

            inputFeature.Geometry = location;
            await inputFeatures.AddFeatureAsync(inputFeature);

            // Create parameters that are passed to the used geoprocessing task
            GeoprocessingParameters viewshedParameters =
                new GeoprocessingParameters(GeoprocessingExecutionType.SynchronousExecute);

            // Request output features in same SpatialReference as view.
            viewshedParameters.OutputSpatialReference = MyMapView.SpatialReference;

            // Add input location to geoprocessing parameters
            viewshedParameters.Inputs.Add("Input_Observation_Point", new GeoprocessingFeatures(inputFeatures));

            // Create job that handles the communication between the application and the geoprocessing task
            var viewshedJob = viewshedTask.CreateJob(viewshedParameters);

            try
            {
                // Execute analysis and wait for the results
                GeoprocessingResult analysisResult = await viewshedJob.GetResultAsync();

                // Get results from the outputs
                GeoprocessingFeatures viewshedResultFeatures = analysisResult.Outputs["Viewshed_Result"] as GeoprocessingFeatures;

                // Add all the results as a graphics to the map
                IFeatureSet viewshedAreas = viewshedResultFeatures.Features;
                foreach (var feature in viewshedAreas)
                {
                    _resultOverlay.Graphics.Add(new Graphic(feature.Geometry));
                }
            }
            catch (Exception ex)
            {
                if (viewshedJob.Status == JobStatus.Failed && viewshedJob.Error != null)
                {
                    MessageBox.Show("Executing geoprocessing failed. " + viewshedJob.Error.Message, "Geoprocessing error");
                }
                else
                {
                    MessageBox.Show("An error occurred. " + ex.ToString(), "Sample error");
                }
            }
            finally
            {
                SetBusy(false);
            }
        }
Exemple #2
0
        private async void AddFeature(MapPoint mapClickPoint)
        {
            // Grab the feature collection from the operational layers and create a feature collection layer
            _featCollectionLayer = (FeatureCollectionLayer)MyMapView.Map.OperationalLayers[0];

            // Get the points table from the feature collection layer and add to the feature collection table for editing
            _featCollectionTable = (FeatureCollectionTable)_featCollectionLayer.Layers[1].FeatureTable;

            // Create a feature
            Feature f = _featCollectionTable.CreateFeature();

            // Populate the feature's geometry with the map point coords
            f.Geometry = mapClickPoint;

            // Add the feature to the feature collection table
            await _featCollectionTable.AddFeatureAsync(f);
        }
        private async void CreateNewFeatureCollection()
        {
            // Create the schema for a points table (one text field to contain a name attribute).
            List <Field> pointFields = new List <Field>();
            Field        placeField  = new Field(FieldType.Text, "Place", "Place Name", 50);

            pointFields.Add(placeField);

            // Create the schema for a lines table (one text field to contain a name attribute).
            List <Field> lineFields    = new List <Field>();
            Field        boundaryField = new Field(FieldType.Text, "Boundary", "Boundary Name", 50);

            lineFields.Add(boundaryField);

            // Create the schema for a polygon table (one text field to contain a name attribute).
            List <Field> polyFields = new List <Field>();
            Field        areaField  = new Field(FieldType.Text, "AreaName", "Area Name", 50);

            polyFields.Add(areaField);

            // Instantiate FeatureCollectionTables with schema and geometry type.
            FeatureCollectionTable pointsTable = new FeatureCollectionTable(pointFields, GeometryType.Point, SpatialReferences.Wgs84);
            FeatureCollectionTable linesTable  = new FeatureCollectionTable(lineFields, GeometryType.Polyline, SpatialReferences.Wgs84);
            FeatureCollectionTable polysTable  = new FeatureCollectionTable(polyFields, GeometryType.Polygon, SpatialReferences.Wgs84);

            // Set rendering for each table.
            pointsTable.Renderer = CreateRenderer(GeometryType.Point);
            linesTable.Renderer  = CreateRenderer(GeometryType.Polyline);
            polysTable.Renderer  = CreateRenderer(GeometryType.Polygon);

            // Create a new point feature, provide geometry and attribute values.
            Feature pointFeature = pointsTable.CreateFeature();

            pointFeature.SetAttributeValue(placeField, "Current location");
            MapPoint point1 = new MapPoint(-79.497238, 8.849289, SpatialReferences.Wgs84);

            pointFeature.Geometry = point1;

            // Create a new line feature, provide geometry and attribute values.
            Feature lineFeature = linesTable.CreateFeature();

            lineFeature.SetAttributeValue(boundaryField, "AManAPlanACanalPanama");
            MapPoint point2 = new MapPoint(-80.035568, 9.432302, SpatialReferences.Wgs84);

            lineFeature.Geometry = new Polyline(new[] { point1, point2 });

            // Create a new polygon feature, provide geometry and attribute values.
            Feature polyFeature = polysTable.CreateFeature();

            polyFeature.SetAttributeValue(areaField, "Restricted area");
            MapPoint point3 = new MapPoint(-79.337936, 8.638903, SpatialReferences.Wgs84);
            MapPoint point4 = new MapPoint(-79.11409, 8.895422, SpatialReferences.Wgs84);

            polyFeature.Geometry = new Polygon(new[] { point1, point3, point4 });

            // Add the new features to the appropriate feature collection table.
            await pointsTable.AddFeatureAsync(pointFeature);

            await linesTable.AddFeatureAsync(lineFeature);

            await polysTable.AddFeatureAsync(polyFeature);

            // Create a feature collection and add the feature collection tables.
            FeatureCollection featuresCollection = new FeatureCollection();

            featuresCollection.Tables.Add(pointsTable);
            featuresCollection.Tables.Add(linesTable);
            featuresCollection.Tables.Add(polysTable);

            // Create a FeatureCollectionLayer.
            FeatureCollectionLayer collectionLayer = new FeatureCollectionLayer(featuresCollection);

            // When the layer loads, zoom the map view to the extent of the feature collection.
            collectionLayer.Loaded += (s, e) => _myMapView.SetViewpointAsync(new Viewpoint(collectionLayer.FullExtent));

            // Add the layer to the Map's Operational Layers collection.
            _myMapView.Map.OperationalLayers.Add(collectionLayer);
        }
        private async void CreateNewFeatureCollection()
        {
            // Create the schema for a points table (one text field to contain a name attribute)
            List <Field> pointFields = new List <Field>();
            Field        placeField  = new Field(FieldType.Text, "Place", "Place Name", 50);

            pointFields.Add(placeField);

            // Create the schema for a lines table (one text field to contain a name attribute)
            List <Field> lineFields    = new List <Field>();
            Field        boundaryField = new Field(FieldType.Text, "Boundary", "Boundary Name", 50);

            lineFields.Add(boundaryField);

            // Create the schema for a polygon table (one text field to contain a name attribute)
            List <Field> polyFields = new List <Field>();
            Field        areaField  = new Field(FieldType.Text, "AreaName", "Area Name", 50);

            polyFields.Add(areaField);

            // Instantiate FeatureCollectionTables with schema and geometry type
            FeatureCollectionTable pointsTable = new FeatureCollectionTable(pointFields, GeometryType.Point, SpatialReferences.Wgs84);
            FeatureCollectionTable linesTable  = new FeatureCollectionTable(lineFields, GeometryType.Polyline, SpatialReferences.Wgs84);
            FeatureCollectionTable polysTable  = new FeatureCollectionTable(polyFields, GeometryType.Polygon, SpatialReferences.Wgs84);

            // Set rendering for each table
            pointsTable.Renderer = CreateRenderer(GeometryType.Point);
            linesTable.Renderer  = CreateRenderer(GeometryType.Polyline);
            polysTable.Renderer  = CreateRenderer(GeometryType.Polygon);

            // Create a new point feature, provide geometry and attribute values
            Feature pointFeature = pointsTable.CreateFeature();

            pointFeature.SetAttributeValue(placeField, "Current location");
            MapPoint point1 = new MapPoint(-79.497238, 8.849289, SpatialReferences.Wgs84);

            pointFeature.Geometry = point1;

            // Create a new line feature, provide geometry and attribute values
            Feature lineFeature = linesTable.CreateFeature();

            lineFeature.SetAttributeValue(boundaryField, "AManAPlanACanalPanama");
            MapPoint point2 = new MapPoint(-80.035568, 9.432302, SpatialReferences.Wgs84);
            Polyline line   = new Polyline(new MapPoint[] { point1, point2 });

            lineFeature.Geometry = line;

            // Create a new polygon feature, provide geometry and attribute values
            Feature polyFeature = polysTable.CreateFeature();

            polyFeature.SetAttributeValue(areaField, "Restricted area");
            MapPoint point3 = new MapPoint(-79.337936, 8.638903, SpatialReferences.Wgs84);
            MapPoint point4 = new MapPoint(-79.11409, 8.895422, SpatialReferences.Wgs84);
            Polygon  poly   = new Polygon(new MapPoint[] { point1, point3, point4 });

            polyFeature.Geometry = poly;

            try
            {
                // Add the new features to the appropriate feature collection table
                await pointsTable.AddFeatureAsync(pointFeature);

                await linesTable.AddFeatureAsync(lineFeature);

                await polysTable.AddFeatureAsync(polyFeature);

                // Create a feature collection and add the feature collection tables
                FeatureCollection featuresCollection = new FeatureCollection();
                featuresCollection.Tables.Add(pointsTable);
                featuresCollection.Tables.Add(linesTable);
                featuresCollection.Tables.Add(polysTable);

                // Create a FeatureCollectionLayer
                FeatureCollectionLayer collectionLayer = new FeatureCollectionLayer(featuresCollection);

                // When the layer loads, zoom the map centered on the feature collection
                await collectionLayer.LoadAsync();

                await _myMapView.SetViewpointCenterAsync(collectionLayer.FullExtent.GetCenter(), 1000000);

                // Add the layer to the Map's Operational Layers collection
                _myMapView.Map.OperationalLayers.Add(collectionLayer);
            }
            catch (Exception e)
            {
                new AlertDialog.Builder(this).SetMessage(e.ToString()).SetTitle("Error").Show();
            }
        }
        private async Task CalculateViewshed(MapPoint location)
        {
            // This function will define a new geoprocessing task that performs a custom viewshed analysis based upon a
            // user click on the map and then display the results back as a polygon fill graphics overlay. If there
            // is a problem with the execution of the geoprocessing task an error message will be displayed

            // Create new geoprocessing task using the url defined in the member variables section
            GeoprocessingTask myViewshedTask = await GeoprocessingTask.CreateAsync(new Uri(_viewshedUrl));

            // Create a new feature collection table based upon point geometries using the current map view spatial reference
            FeatureCollectionTable myInputFeatures = new FeatureCollectionTable(new List <Field>(), GeometryType.Point, MyMapView.SpatialReference);

            // Create a new feature from the feature collection table. It will not have a coordinate location (x,y) yet
            Feature myInputFeature = myInputFeatures.CreateFeature();

            // Assign a physical location to the new point feature based upon where the user clicked in the map view
            myInputFeature.Geometry = location;

            // Add the new feature with (x,y) location to the feature collection table
            await myInputFeatures.AddFeatureAsync(myInputFeature);

            // Create the parameters that are passed to the used geoprocessing task
            GeoprocessingParameters myViewshedParameters =
                new GeoprocessingParameters(GeoprocessingExecutionType.SynchronousExecute)
            {
                // Request the output features to use the same SpatialReference as the map view
                OutputSpatialReference = MyMapView.SpatialReference
            };

            // Add an input location to the geoprocessing parameters
            myViewshedParameters.Inputs.Add("Input_Observation_Point", new GeoprocessingFeatures(myInputFeatures));

            // Create the job that handles the communication between the application and the geoprocessing task
            GeoprocessingJob myViewshedJob = myViewshedTask.CreateJob(myViewshedParameters);

            try
            {
                // Execute analysis and wait for the results
                GeoprocessingResult myAnalysisResult = await myViewshedJob.GetResultAsync();

                // Get the results from the outputs
                GeoprocessingFeatures myViewshedResultFeatures = (GeoprocessingFeatures)myAnalysisResult.Outputs["Viewshed_Result"];

                // Add all the results as a graphics to the map
                IFeatureSet myViewshedAreas = myViewshedResultFeatures.Features;
                foreach (Feature myFeature in myViewshedAreas)
                {
                    _resultOverlay.Graphics.Add(new Graphic(myFeature.Geometry));
                }
            }
            catch (Exception ex)
            {
                // Display an error message if there is a problem
                if (myViewshedJob.Status == JobStatus.Failed && myViewshedJob.Error != null)
                {
                    MessageBox.Show("Executing geoprocessing failed. " + myViewshedJob.Error.Message, "Geoprocessing error");
                }
                else
                {
                    MessageBox.Show("An error occurred. " + ex.ToString(), "Sample error");
                }
            }
            finally
            {
                // Indicate that the geoprocessing is not running
                SetBusy(false);
            }
        }
        private async Task CalculateViewshed(MapPoint location)
        {
            // This function will define a new geoprocessing task that performs a custom viewshed analysis based upon a
            // user click on the map and then display the results back as a polygon fill graphics overlay. If there
            // is a problem with the execution of the geoprocessing task an error message will be displayed.

            // Create new geoprocessing task using the URL defined in the member variables section.
            GeoprocessingTask viewshedTask = await GeoprocessingTask.CreateAsync(new Uri(ViewshedServiceUrl));

            // Create a new feature collection table based upon point geometries using the current map view spatial reference.
            FeatureCollectionTable inputFeatures = new FeatureCollectionTable(new List <Field>(), GeometryType.Point, _myMapView.SpatialReference);

            // Create a new feature from the feature collection table. It will not have a coordinate location (x,y) yet.
            Feature inputFeature = inputFeatures.CreateFeature();

            // Assign a physical location to the new point feature based upon where the user clicked in the map view.
            inputFeature.Geometry = location;

            // Add the new feature with (x,y) location to the feature collection table.
            await inputFeatures.AddFeatureAsync(inputFeature);

            // Create the parameters that are passed to the used geoprocessing task.
            GeoprocessingParameters viewshedParameters = new GeoprocessingParameters(GeoprocessingExecutionType.SynchronousExecute)
            {
                OutputSpatialReference = _myMapView.SpatialReference
            };

            // Add an input location to the geoprocessing parameters.
            viewshedParameters.Inputs.Add("Input_Observation_Point", new GeoprocessingFeatures(inputFeatures));

            // Create the job that handles the communication between the application and the geoprocessing task.
            GeoprocessingJob viewshedJob = viewshedTask.CreateJob(viewshedParameters);

            try
            {
                // Execute analysis and wait for the results.
                GeoprocessingResult analysisResult = await viewshedJob.GetResultAsync();

                // Get the results from the outputs.
                GeoprocessingFeatures viewshedResultFeatures = (GeoprocessingFeatures)analysisResult.Outputs["Viewshed_Result"];

                // Add all the results as a graphics to the map.
                foreach (Feature feature in viewshedResultFeatures.Features)
                {
                    _resultOverlay.Graphics.Add(new Graphic(feature.Geometry));
                }
            }
            catch (Exception ex)
            {
                // Display an error message if there is a problem.
                if (viewshedJob.Status == JobStatus.Failed && viewshedJob.Error != null)
                {
                    // Report error
                    UIAlertController alert = UIAlertController.Create("Geoprocessing Error", viewshedJob.Error.Message, UIAlertControllerStyle.Alert);
                    alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                    PresentViewController(alert, true, null);
                }
                else
                {
                    // Report error
                    UIAlertController alert = UIAlertController.Create("Sample Error", ex.ToString(), UIAlertControllerStyle.Alert);
                    alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                    PresentViewController(alert, true, null);
                }
            }
        }
        private async void CreateNewFeatureCollection()
        {
            // Create the schema for a points table (one text field to contain a name attribute)
            List<Field> pointFields = new List<Field>();
            Field placeField = new Field(FieldType.Text, "Place", "Place Name", 50);
            pointFields.Add(placeField);

            // Create the schema for a lines table (one text field to contain a name attribute)
            List<Field> lineFields = new List<Field>();
            Field boundaryField = new Field(FieldType.Text, "Boundary", "Boundary Name", 50);
            lineFields.Add(boundaryField);

            // Create the schema for a polygon table (one text field to contain a name attribute)
            List<Field> polyFields = new List<Field>();
            Field areaField = new Field(FieldType.Text, "AreaName", "Area Name", 50);
            polyFields.Add(areaField);

            // Instantiate FeatureCollectionTables with schema and geometry type
            FeatureCollectionTable pointsTable = new FeatureCollectionTable(pointFields, GeometryType.Point, SpatialReferences.Wgs84);
            FeatureCollectionTable linesTable = new FeatureCollectionTable(lineFields, GeometryType.Polyline, SpatialReferences.Wgs84);
            FeatureCollectionTable polysTable = new FeatureCollectionTable(polyFields, GeometryType.Polygon, SpatialReferences.Wgs84);

            // Set rendering for each table
            pointsTable.Renderer = CreateRenderer(GeometryType.Point);
            linesTable.Renderer = CreateRenderer(GeometryType.Polyline);
            polysTable.Renderer = CreateRenderer(GeometryType.Polygon);

            // Create a new point feature, provide geometry and attribute values
            Feature pointFeature = pointsTable.CreateFeature();
            pointFeature.SetAttributeValue(placeField, "Current location");
            MapPoint point1 = new MapPoint(-79.497238, 8.849289, SpatialReferences.Wgs84);
            pointFeature.Geometry = point1;

            // Create a new line feature, provide geometry and attribute values
            Feature lineFeature = linesTable.CreateFeature();
            lineFeature.SetAttributeValue(boundaryField, "AManAPlanACanalPanama");
            MapPoint point2 = new MapPoint(-80.035568, 9.432302, SpatialReferences.Wgs84);
            Polyline line = new Polyline(new MapPoint[] { point1, point2 });
            lineFeature.Geometry = line;

            // Create a new polygon feature, provide geometry and attribute values
            Feature polyFeature = polysTable.CreateFeature();
            polyFeature.SetAttributeValue(areaField, "Restricted area");
            MapPoint point3 = new MapPoint(-79.337936, 8.638903, SpatialReferences.Wgs84);
            MapPoint point4 = new MapPoint(-79.11409, 8.895422, SpatialReferences.Wgs84);
            Polygon poly = new Polygon(new MapPoint[] { point1, point3, point4 });
            polyFeature.Geometry = poly;

            // Add the new features to the appropriate feature collection table 
            await pointsTable.AddFeatureAsync(pointFeature);
            await linesTable.AddFeatureAsync(lineFeature);
            await polysTable.AddFeatureAsync(polyFeature);

            // Create a feature collection and add the feature collection tables
            FeatureCollection featuresCollection = new FeatureCollection();
            featuresCollection.Tables.Add(pointsTable);
            featuresCollection.Tables.Add(linesTable);
            featuresCollection.Tables.Add(polysTable);

            // Create a FeatureCollectionLayer 
            FeatureCollectionLayer collectionLayer = new FeatureCollectionLayer(featuresCollection);

            // When the layer loads, zoom the map view to the extent of the feature collection
            collectionLayer.Loaded += CollectionLayer_Loaded;
            // Add the layer to the Map's Operational Layers collection
            MyMapView.Map.OperationalLayers.Add(collectionLayer);
        }
Exemple #8
0
        private async void CreateFeatureCollectionLayerFromNYTimesArticles()
        {
            // Read the NY Times data from the text file and get back a list of each article.
            // The format of each record/article will look like:
            // [Article summary]~[Article abstract]~[Country name]~[Url to the NY times article]~[Url to an image about the NY times article]~[Date of NY Times news article]
            // Ex:
            // Netanyahu not happy with Cohen~A spokesman for Prime Minister Benjamin Netanyahu disagrees with Roger Cohen’s “pessimism.”~Israel~https://www.nytimes.com/2018/01/02/opinion/israel-future.html~https://www.nytimes.com/images/2017/12/29/opinion/29cohenWeb/29cohenWeb-thumbLarge.jpg~20180102
            List <string> myNYTimesArticles = ReadTextFile3(_NEWSFILE);

            // Get the collection of all the layers in the map.
            var myMapAllLayers = MyMapView.Map.AllLayers;

            // Create a place holder for the world countries feature layer.
            FeatureLayer myFeatureLayer = null;

            // Loop through all of the layers.
            foreach (var myLayer in myMapAllLayers)
            {
                // Get the Id of the layer.
                string myLayerName = myLayer.Id;

                // If the layer id matches world countries set that to the feature layer.
                if (myLayerName == "WorldCountries")
                {
                    myFeatureLayer = (FeatureLayer)myLayer;
                }
            }

            // Get the feature table from the world countries shape file feature layer.
            FeatureTable myFeatureTable = myFeatureLayer.FeatureTable;

            // Create a new query parameters.
            QueryParameters myQueryParameters = new QueryParameters();

            // Define the where clause for the query parameters. It will select all the records in the world countries shape file feature table.
            myQueryParameters.WhereClause = "1 = 1";

            // Execute the feature query and get the results back. It will select all the records in the world countries shape file feature table.
            FeatureQueryResult myFeatureQueryResult = await myFeatureTable.QueryFeaturesAsync(myQueryParameters);

            // Create the schema for the polygon feature collection table.
            List <Field> myFeatureCollectionAttributeFields = new List <Field>();

            // Create a field for the feature collection layer. It will contain a text field called area name that is the county name.
            Field myAreaNameField = new Field(FieldType.Text, "AreaName", "Area Name", 50);

            // Add the country name field to the list of fields that will be added to the feature collection table.
            myFeatureCollectionAttributeFields.Add(myAreaNameField);

            // Create the feature collection table based on the list of attribute fields, a polygons feature type
            FeatureCollectionTable myFeatureCollectionTable = new FeatureCollectionTable(myFeatureCollectionAttributeFields, GeometryType.Polygon, SpatialReferences.Wgs84);

            // Create the outline symbol for the country fill symbol.
            SimpleLineSymbol mySimpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.DarkBlue, 2);

            // Create the fill symbol for the country. Solid yellow, with dark blue outline.
            SimpleFillSymbol mySimpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.Solid, Color.Yellow, mySimpleLineSymbol);

            // Set the renderer of the feature collection table to be the simple fill symbol.
            myFeatureCollectionTable.Renderer = new SimpleRenderer(mySimpleFillSymbol);

            // Loop through each feature in the returned feature query results of the world countries shape file
            foreach (Feature myFeature in myFeatureQueryResult)
            {
                // Get the geometry (aka shape) for one feature.
                Geometry myGeometry = myFeature.Geometry;

                // Get the value (aka. the text for the record of a specific field).
                var myValue = (string)myFeature.Attributes["NAME"];

                // Loop through each NY Times article.
                foreach (string oneNYTimesArticle in myNYTimesArticles)
                {
                    // Remove an embedded double quotes that may be in or surrounding the country name in the NY Times data base.
                    char[] charsToTrim = { '"' };
                    string country     = oneNYTimesArticle.Split('~')[2].Trim(charsToTrim);

                    // Find a match from the shape file country feature and the NY Times country name in the article.
                    if ((string)myValue == country)
                    {
                        // Create a new polygon feature, provide geometry and attribute values.
                        Feature polyFeature = myFeatureCollectionTable.CreateFeature();
                        polyFeature.SetAttributeValue(myAreaNameField, country);
                        polyFeature.Geometry = myGeometry;

                        // Add the new features to the appropriate feature collection table.
                        await myFeatureCollectionTable.AddFeatureAsync(polyFeature);

                        // Once we have found a matching country in the subset news article file that matches a shape file record
                        // there is no need to find another one since we have just created a record in the FeatureCollectionTable.
                        break;
                    }
                }
            }

            // Create a feature collection and add the feature collection tables
            FeatureCollection myFeatureCollection = new FeatureCollection();

            myFeatureCollection.Tables.Add(myFeatureCollectionTable);

            // Create a FeatureCollectionLayer
            FeatureCollectionLayer myFeatureCollectionLayer = new FeatureCollectionLayer(myFeatureCollection);

            myFeatureCollectionLayer.Id   = "Joined"; // might not be needed
            myFeatureCollectionLayer.Name = "JoinedFCL";

            // When the layer loads, zoom the map centered on the feature collection
            await myFeatureCollectionLayer.LoadAsync();

            // Add the layer to the Map's Operational Layers collection
            MyMapView.Map.OperationalLayers.Add(myFeatureCollectionLayer);
        }
        private async Task CalculateViewshed(MapPoint location)
        {
            // This function will define a new geoprocessing task that performs a custom viewshed analysis based upon a 
            // user click on the map and then display the results back as a polygon fill graphics overlay. If there
            // is a problem with the execution of the geoprocessing task an error message will be displayed 

            // Create new geoprocessing task using the url defined in the member variables section
            var myViewshedTask = new GeoprocessingTask(new Uri(_viewshedUrl));

            // Create a new feature collection table based upon point geometries using the current map view spatial reference
            var myInputFeatures = new FeatureCollectionTable(new List<Field>(), GeometryType.Point, MyMapView.SpatialReference);

            // Create a new feature from the feature collection table. It will not have a coordinate location (x,y) yet
            Feature myInputFeature = myInputFeatures.CreateFeature();

            // Assign a physical location to the new point feature based upon where the user clicked in the map view
            myInputFeature.Geometry = location;

            // Add the new feature with (x,y) location to the feature collection table
            await myInputFeatures.AddFeatureAsync(myInputFeature);

            // Create the parameters that are passed to the used geoprocessing task
            GeoprocessingParameters myViewshedParameters =
                new GeoprocessingParameters(GeoprocessingExecutionType.SynchronousExecute);

            // Request the output features to use the same SpatialReference as the map view
            myViewshedParameters.OutputSpatialReference = MyMapView.SpatialReference;

            // Add an input location to the geoprocessing parameters
            myViewshedParameters.Inputs.Add("Input_Observation_Point", new GeoprocessingFeatures(myInputFeatures));

            // Create the job that handles the communication between the application and the geoprocessing task
            var myViewshedJob = myViewshedTask.CreateJob(myViewshedParameters);

            try
            {
                // Execute analysis and wait for the results
                GeoprocessingResult myAnalysisResult = await myViewshedJob.GetResultAsync();

                // Get the results from the outputs
                GeoprocessingFeatures myViewshedResultFeatures = myAnalysisResult.Outputs["Viewshed_Result"] as GeoprocessingFeatures;

                // Add all the results as a graphics to the map
                IFeatureSet myViewshedAreas = myViewshedResultFeatures.Features;
                foreach (var myFeature in myViewshedAreas)
                {
                    _resultOverlay.Graphics.Add(new Graphic(myFeature.Geometry));
                }
            }
            catch (Exception ex)
            {
                // Display an error message if there is a problem
                if (myViewshedJob.Status == JobStatus.Failed && myViewshedJob.Error != null)
                {
                    var message = new MessageDialog("Executing geoprocessing failed. " + myViewshedJob.Error.Message, "Geoprocessing error");
                    await message.ShowAsync();
                }
                else
                {
                    var message = new MessageDialog("An error occurred. " + ex.ToString(), "Sample error");
                    await message.ShowAsync();
                }
            }
            finally
            {
                // Indicate that the geoprocessing is not running
                SetBusy(false);
            }
        }
Exemple #10
0
        private async void ExecuteGPService()
        {
            //加载DEM并且缩放到该区域
            Map    map          = new Map(Basemap.CreateTopographic());
            string pathToRaster = @"D:\work\github\ExecuteGPK\LasVegasNED13_geoid1.tif";
            var    myRaster     = new Raster(pathToRaster);
            // create a RasterLayer using the Raster
            var newRasterLayer = new RasterLayer(myRaster);

            map.OperationalLayers.Add(newRasterLayer);
            Viewpoint viewPoint = new Viewpoint(36.131, -115.144, 800000);

            myMapView.Map = map;
            await myMapView.SetViewpointAsync(viewPoint, TimeSpan.FromSeconds(2));

            StartLocalServer();
            LocalGeoprocessingService localServiceGP = new LocalGeoprocessingService(@"D:\work\github\ExecuteGPK\InterpolateShape.gpk");

            localServiceGP.ServiceType = GeoprocessingServiceType.SynchronousExecute;
            // Handle the status changed event to check when it's loaded
            localServiceGP.StatusChanged += async(svc, args) =>
            {
                // If service started successfully, create a gp task
                if (args.Status == LocalServerStatus.Started)
                {
                    // Get the URL for the specific geoprocessing tool
                    var gpSvcUrl = (svc as LocalGeoprocessingService).Url.AbsoluteUri + "/InterpolateShape";
                    // Create the geoprocessing task
                    GeoprocessingTask       gpRouteTask = new GeoprocessingTask(new Uri(gpSvcUrl));
                    GeoprocessingParameters para        = new GeoprocessingParameters(GeoprocessingExecutionType.SynchronousExecute);
                    // Create the schema for a lines table (one text field to contain a name attribute)
                    var     inputFeatures = new FeatureCollectionTable(new List <Field>(), GeometryType.Polyline, myMapView.SpatialReference);
                    Feature inputFeature  = inputFeatures.CreateFeature();
                    var     geometry      = await myMapView.SketchEditor.StartAsync(SketchCreationMode.Polyline, false);

                    inputFeature.Geometry = geometry;
                    await inputFeatures.AddFeatureAsync(inputFeature);

                    para.Inputs.Add("inputLine", new GeoprocessingFeatures(inputFeatures));
                    para.ReturnZ = true;
                    para.OutputSpatialReference = myMapView.SpatialReference;

                    GeoprocessingJob routeJob = gpRouteTask.CreateJob(para);

                    try
                    {
                        // Execute analysis and wait for the results
                        GeoprocessingResult geoprocessingResult = await routeJob.GetResultAsync();

                        GeoprocessingFeatures resultFeatures               = geoprocessingResult.Outputs["outputLine"] as GeoprocessingFeatures;
                        IFeatureSet           interpolateShapeResult       = resultFeatures.Features;
                        Esri.ArcGISRuntime.Geometry.Polyline elevationLine = interpolateShapeResult.First().Geometry as Esri.ArcGISRuntime.Geometry.Polyline;
                        MapPoint startPoint = elevationLine.Parts[0].Points[0];
                        int      count      = elevationLine.Parts[0].PointCount;
                        MapPoint stopPoint  = elevationLine.Parts[0].Points[count - 1];
                        double   chazhi     = stopPoint.Z - startPoint.Z;
                        MessageBox.Show("终点的Z值为: " + stopPoint.Z.ToString() + ",起点的Z值为: " + startPoint.Z.ToString());
                    }
                    catch (Exception ex)
                    {
                        if (routeJob.Status == JobStatus.Failed && routeJob.Error != null)
                        {
                            MessageBox.Show("Executing geoprocessing failed. " + routeJob.Error.Message, "Geoprocessing error");
                        }
                        else
                        {
                            MessageBox.Show("An error occurred. " + ex.ToString(), "Sample error");
                        }
                    }
                    // Create parameters, run the task, process results, etc.
                    // ...
                }
            };
            // Start the local geoprocessing service
            await localServiceGP.StartAsync();
        }