Draw a Line
<%
// Get the parameters passed in from the task pane
String x0 = Request.Params["x0"];
String y0 = Request.Params["y0"];
String x1 = Request.Params["x1"];
String y1 = Request.Params["y1"];
sessionId = Request.Params["SESSION"];
mapName = Request.Params["MAPNAME"];
try
{
// --------------------------------------------------//
// Basic initialization needs to be done every time.
MapGuideApi.MgInitializeWebTier(Constants.WebConfigPath);
// Get the user information using the session id,
// and set up a connection to the site server.
MgUserInformation userInfo = new MgUserInformation(sessionId);
MgSiteConnection siteConnection = new MgSiteConnection();
siteConnection.Open(userInfo);
// Get an instance of the required service(s).
MgResourceService resourceService = (MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService);
MgFeatureService featureService = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService);
//---------------------------------------------------//
// Open the map
MgMap map = new MgMap(siteConnection);
map.Open(mapName);
String layerName = "Lines";
String layerLegendLabel = "New Lines";
String groupName = "Analysis";
String groupLegendLabel = "Analysis";
//---------------------------------------------------//
// Does the temporary feature source already exist?
// If not, create it
String featureSourceName = "Session:" + sessionId + "//TemporaryLines.FeatureSource";
MgResourceIdentifier resourceIdentifier = new MgResourceIdentifier(featureSourceName);
bool featureSourceExists = DoesResourceExist(resourceIdentifier, resourceService);
if (! featureSourceExists)
{
// Create a temporary feature source to draw the lines on
// Create a feature class definition for the new feature
// source
MgClassDefinition classDefinition = new MgClassDefinition();
classDefinition.SetName("Lines");
classDefinition.SetDescription("Lines to display.");
String geometryPropertyName="SHPGEOM";
classDefinition.SetDefaultGeometryPropertyName( geometryPropertyName);
// Create an identify property
MgDataPropertyDefinition identityProperty = new MgDataPropertyDefinition("KEY");
identityProperty.SetDataType(MgPropertyType.Int32);
identityProperty.SetAutoGeneration(true);
identityProperty.SetReadOnly(true);
// Add the identity property to the class definition
classDefinition.GetIdentityProperties().Add(identityProperty);
classDefinition.GetProperties().Add(identityProperty);
// Create a name property
MgDataPropertyDefinition nameProperty = new MgDataPropertyDefinition("NAME");
nameProperty.SetDataType(MgPropertyType.String);
// Add the name property to the class definition
classDefinition.GetProperties().Add(nameProperty);
// Create a geometry property
MgGeometricPropertyDefinition geometryProperty = new MgGeometricPropertyDefinition(geometryPropertyName);
geometryProperty.SetGeometryTypes(MgFeatureGeometricType.Surface);
// Add the geometry property to the class definition
classDefinition.GetProperties().Add(geometryProperty);
// Create a feature schema
MgFeatureSchema featureSchema = new MgFeatureSchema("SHP_Schema", "Line schema");
// Add the feature schema to the class definition
featureSchema.GetClasses().Add(classDefinition);
// Create the feature source
String wkt = map.GetMapSRS();
MgFileFeatureSourceParams sdfParams = new MgFileFeatureSourceParams("OSGeo.SDF", "spatial context", wkt, featureSchema);
featureService.CreateFeatureSource(resourceIdentifier, sdfParams);
}
// Add the line to the feature source
MgBatchPropertyCollection batchPropertyCollection = new MgBatchPropertyCollection();
MgPropertyCollection propertyCollection = MakeLine("Line A", x0, y0, x1, y1);
batchPropertyCollection.Add(propertyCollection);
// Add the batch property collection to the feature source
MgInsertFeatures cmd = new MgInsertFeatures(layerName, batchPropertyCollection);
MgFeatureCommandCollection featureCommandCollection = new MgFeatureCommandCollection();
featureCommandCollection.Add(cmd);
// Execute the "add" commands
featureService.UpdateFeatures(resourceIdentifier, featureCommandCollection, false);
//---------------------------------------------------//
bool layerExists = DoesLayerExist(layerName, map);
if (! layerExists )
{
// Create a new layer which uses that feature source
// Create a line rule to stylize the lines
String ruleLegendLabel = "Lines Rule";
String filter = "";
String color = "FF0000FF";
LayerDefinitionFactory factory = new LayerDefinitionFactory(Server);
String lineRule = factory.CreateLineRule(ruleLegendLabel, filter, color);
// Create a line type style
String lineTypeStyle = factory.CreateLineTypeStyle(lineRule);
// Create a scale range
String minScale = "0";
String maxScale = "1000000000000";
String lineScaleRange = factory.CreateScaleRange(minScale, maxScale, lineTypeStyle);
// Create the layer definiton
String featureName = "SHP_Schema:Lines";
String geometry = "SHPGEOM";
String layerDefinition = factory.CreateLayerDefinition(featureSourceName, featureName, geometry, lineScaleRange);
//---------------------------------------------------//
// Add the layer to the map
XmlDocument doc = new XmlDocument();
doc.LoadXml(layerDefinition);
MgLayer newLayer = LayerFunctions.AddLayerDefinitionToMap(doc, layerName, layerLegendLabel, sessionId, resourceService, map);
// Add the layer to a layer group
LayerFunctions.AddLayerToGroup(newLayer,groupName, groupLegendLabel, map);
}
// --------------------------------------------------//
// Turn on the visibility of this layer.
// (If the layer does not already exist in the map, it will be visible by default when it is added.
// But if the user has already run this script, he or she may have set the layer to be invisible.)
MgLayerCollection layerCollection = map.GetLayers();
if (layerCollection.Contains(layerName))
{
MgLayer linesLayer = (MgLayer)layerCollection.GetItem(layerName);
linesLayer.SetVisible(true);
}
MgLayerGroupCollection groupCollection = map.GetLayerGroups();
if (groupCollection.Contains(groupName))
{
MgLayerGroup analysisGroup =groupCollection.GetItem(groupName);
analysisGroup.SetVisible(true);
}
//---------------------------------------------------//
// Save the map back to the session repository
map.Save();
//---------------------------------------------------//
}
catch (MgException e)
{
Response.Write( " \n");
}
%>