private async void GetFeaturesFromQuery()
        {
            // Create a service feature table to get features from.
            ServiceFeatureTable featTable = new ServiceFeatureTable(new Uri(FeatureLayerUrl));

            // Create a query to get all features in the table.
            QueryParameters queryParams = new QueryParameters
            {
                WhereClause = "1=1"
            };

            // Query the table to get all features.
            FeatureQueryResult featureResult = await featTable.QueryFeaturesAsync(queryParams);

            // Create a new feature collection table from the result features.
            FeatureCollectionTable collectTable = new FeatureCollectionTable(featureResult);

            // Create a feature collection and add the table.
            FeatureCollection featCollection = new FeatureCollection();

            featCollection.Tables.Add(collectTable);

            // Create a layer to display the feature collection, add it to the map's operational layers.
            FeatureCollectionLayer featureCollectionLayer = new FeatureCollectionLayer(featCollection);

            _myMapView.Map.OperationalLayers.Add(featureCollectionLayer);

            // Zoom to the extent of the feature collection layer.
            await featureCollectionLayer.LoadAsync();

            await _myMapView.SetViewpointGeometryAsync(featureCollectionLayer.FullExtent, 50);
        }
        private async void SaveFeatureCollectionFromQuery()
        {
            Debug.WriteLine("Creating feature collection from the service...");

            // Create a service feature table to get features from
            ServiceFeatureTable featTable = new ServiceFeatureTable(new Uri(FeatureLayerUrl));

            // Create a query to get all features in the table
            QueryParameters queryParams = new QueryParameters();

            queryParams.WhereClause = "1=1";

            // Query the table to get all features
            FeatureQueryResult featureResult = await featTable.QueryFeaturesAsync(queryParams);

            // Create a new feature collection table from the result features
            FeatureCollectionTable collectTable = new FeatureCollectionTable(featureResult);

            // Create a feature collection and add the table
            FeatureCollection featCollection = new FeatureCollection();

            featCollection.Tables.Add(collectTable);


            var tokenServiceUri = new Uri("https://www.arcgis.com/sharing/rest");  // url for generating token

            AuthenticationManager authManager = AuthenticationManager.Current;
            var cred = await authManager.GenerateCredentialAsync(tokenServiceUri, "*******", "*******");

            // Open a portal item containing a feature collection
            ArcGISPortal portal = await ArcGISPortal.CreateAsync(tokenServiceUri, cred);

            List <string> str = new List <string>();

            str.Add("Save Feature collection");


            FeatureCollectionLayer featCollectionTable = new FeatureCollectionLayer(featCollection);
            await featCollectionTable.LoadAsync();

            MyMapView.Map.OperationalLayers.Add(featCollectionTable);

            Debug.WriteLine("Create a feature layer named, Feature Collection to Portal, and saving to portal...");

            try
            {
                await featCollection.SaveAsAsync(portal, null, "Feature Collection to Portal", "FeatureCollection", str);

                MessageBox.Show("Feature Collection saved to portal");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 3
0
        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)
            {
                await Application.Current.MainPage.DisplayAlert("Error", e.ToString(), "OK");
            }
        }
Exemplo n.º 4
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);
        }