Working with Feature Layers (Importing and Performing Spatial Queries)
This example demonstrates how to import a feature layer with its attributes into a project and select a symbol type for the features. It shows how to base layer properties on attribute values and how to perform a spatial query. This example uses ICreator80 (CreateFeatureLayer), IFeatureLayer80 (DataSourceInfo, Position, Streaming, FeatureGroups, ExecuteSpatialQuery, Load), IFeatureGroups80 (Point), IFeatureGroup80 (DisplayAs, SetProperty), IDataSourceInfo80, IAttributes80 (ImportAll), IAttribute80 (Type), IFeatures80, IGeometry, ILinearRing, IGeometryCreator (CreateLinearRingGeometry, CreatePolygonGeometry), IPosition80 (Pitch, X, Y, Distance), and INavigate80 (FlyTo) properties and methods.
private void PointLayer()
{
string tMsg = String.Empty;
IFeatureLayer80 cFeatureLayer = null;
IFeatureGroups80 cFeatureLayerGroups = null;
IFeatureGroup80 cFeatureGroupPoint = null;
try
{
//
// A. Instantiate TerraExplorer Object
//
var SGWorld = new SGWorld80();
//
// B. Create shapefile layer
//
{
// B1. Generate shapefile connection string
string tShapeFileName = @" C:\Program Files\Skyline\TerraExplorer Pro\Tools\Data-Library\Layers\Capitals\data\World_Capital.shp ";
string tConnectionString = String.Format("FileName={0};TEPlugName=OGR;", tShapeFileName);
// B2. Create point feature layer
cFeatureLayer = SGWorld.Creator.CreateFeatureLayer("Capitals", tConnectionString);
}
//
// C. Display points as text label & set label text property to one of the shape text type attribute
//
{
// C1. Set Point FeatureGroup DisplayAs property to label
cFeatureLayerGroups = cFeatureLayer.FeatureGroups;
cFeatureGroupPoint = cFeatureLayerGroups.Point;
cFeatureGroupPoint.DisplayAs = ObjectTypeCode.OT_LABEL;
// C2. Set label "Scale" property for visualization from high altitude
{
double dScale = 10000.0;
cFeatureGroupPoint.SetProperty("Scale", dScale);
}
// C3. Set label "Text" property to the first found text attribute "Name" property
{
// Get layer data source attributes
var cFeatureLayerDataSource = cFeatureLayer.DataSourceInfo;
var cAttributes = cFeatureLayerDataSource.Attributes;
// Import all data source attributes
cFeatureLayerDataSource.Attributes.ImportAll = true;
// Iterate attributes, find first attribute of type text and set its name to label text
foreach (IAttribute80 cAttribute incFeatureLayerDataSource.Attributes)
{
if (cAttribute.Type == AttributeTypeCode.AT_TEXT)
{
cFeatureGroupPoint.SetProperty("Text", String.Format("[{0}]", cAttribute.Name));
break;
}
}
}
}
//
// D. Perform spatial query
//
{
// D1. Generate polygon geometry (Part of Europe)
List<double> cSQVerticesArrayDbl = new List<double>() { -10.0,50.0,0.0,
-10.0,30.0,0.0,
30.0,30.0,0.0,
30.0,50.0,0.0 };
// D2. Create polygon geometry from linear ring and call spatial query
IFeatures80 cSQResFeatures = null;
IGeometry cSQPlgGeometry = null;
ILinearRing cSQRing = SGWorld.Creator.GeometryCreator.CreateLinearRingGeometry(cSQVerticesArrayDbl.ToArray());
cSQPlgGeometry = SGWorld.Creator.GeometryCreator.CreatePolygonGeometry(cSQRing, null);
cSQResFeatures = cFeatureLayer.ExecuteSpatialQuery(cSQPlgGeometry, IntersectionType.IT_INTERSECT);
}
//
// E. Load Feature layer in "Entire" mode
//
{
cFeatureLayer.Streaming = false;
cFeatureLayer.Load();
}
//
// F. FlyTo feature layer view point
//
{
var cFlyToPos = cFeatureLayer.Position.Copy();
cFlyToPos.Pitch = -89.0; // Set camera to look downward on polygon
cFlyToPos.X = 10.50;
cFlyToPos.Y = 47.50;
cFlyToPos.Distance = 3500000;
SGWorld.Navigate.FlyTo(cFlyToPos, ActionCode.AC_FLYTO);
}
}
catch (Exception ex)
{
tMsg = String.Format("PointLayerButton_Click Exception: {0}", ex.Message);
MessageBox.Show(tMsg);
}
}