Preview
Hi, Visitor  | 

Get Feature Sample

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Description

Demonstrates how to retrieve a feature from a shapefile. An application can request one or more features from a shapefile by specifying the desired feature ids in the fid parameter of the request Uri, as shown in the sample code.

Sample Details

The code in the Browse Code section does the following:
  1. Creates a UriQueryBuilder instance which is used to create a REST Uri with the appropriate parameters.
  2. Creates a RESTHelper instance that provides methods to easily communicate with ShapeServer's REST endpoints.
  3. Retrieves the feature response in a ShapefileFeatureCollection object.
  4. Enumerates through the collection of ShapefileFeature types.
  5. Casts the feature to its actual type to perform feature specific actions.
Note
Replace the [YourApiKey] parameter value with your actual API key.

Sample Instructions

Prerequisites

To run this sample, you will need to download the .NET SDK for ShapeServer. The SDK contains the assemblies necessary to successfully run the sample. Use Microsoft Visual Studio 2012 (Express) to build and run a sample. To obtain an evaluation copy, go to Visual Studio 2012.

Build the sample

  1. Start Visual Studio 2012 (Express) and select File > New > Project.
  2. In the language of your choice, select Windows and click Console Application.
  3. Enter a name for the sample and click OK to create the project.
  4. In the Solution Explorer window, right-click the References node and select Add reference.
  5. Browse to the location of the ShapeServer SDK files and select the following assemblies:
    • BartelsOnline.ShapeServer.SDK
    • BartelsOnline.ShapeServer.REST.Types
    • BartelsOnline.Shapefiles.Types
  6. Copy the code in the Browse Code section and paste it in the project.
  7. Press F6 or use Build > Build Solution to build the sample.

Run the sample

To debug the sample and then run it, press F5 or use Debug > Start Debugging.
To run the sample without debugging, press Ctrl+F5 or use Debug > Start Without Debugging.

Interactive Sample

An interactive sample is available for this code. You will need to sign-in to able to run the interactive sample.

Browse Code

C#
VB
        
using System;
using BartelsOnline.ShapeServer.SDK.REST;
using BartelsOnline.ShapeServer.REST.Types.Responses;

namespace BartelsOnline.ShapeServer.Samples
{
   class Program
   {
      static void Main(string[] args)
      {
         try
         {
            // Get data from file 'countries'
            string file = "countries";

            UriQueryBuilder uriQueryBuilder = new UriQueryBuilder("http://api.shapeserver.com/REST/v1/");
            uriQueryBuilder.Add("key", "[YourApiKey]");

            // Get feature data of The Netherlands
            uriQueryBuilder.Add("fid", "111");

            Uri featureUri = uriQueryBuilder.ToUri("shapefiles/" + file);
            RESTHelper helper = new RESTHelper();
            Response response = helper.GetData(featureUri);

            ShapefileFeatureCollection col = (ShapefileFeatureCollection)response.Resource;
            foreach (ShapefileFeature shapefileFeature in col.ShapefileFeatures)
            {
               BartelsOnline.Shapefiles.Types.Feature feature = shapefileFeature.Feature;

               Console.WriteLine("===================================");
               Console.WriteLine("Feature type: " + feature.ToString());
               Console.WriteLine("Feature ID: " + feature.FeatureId);
               Console.WriteLine("Well-Known Text: " + feature.WKT);

               // Cast the feature to its actual type to perform feature specific actions.
               if (feature is BartelsOnline.Shapefiles.Types.Polygon)
               {
                  BartelsOnline.Shapefiles.Types.Polygon polygon = 
					 feature as BartelsOnline.Shapefiles.Types.Polygon;
                  Console.WriteLine("Interior ring count: " + polygon.InteriorRings.Count.ToString());
               }
            }
         }
         catch (Exception exp)
         {
            Console.WriteLine(exp.Message);
         }
         finally
         {
            Console.ReadLine();
         }
      }
   }
}    
    

See Also