Hello All,
I am trying to add a new Point to the Session Layer. Here is my code.
For some reason I am not seeing the point.
Any suggestions please ?
Thanks
Anand
private void DrawPoint(string InPointID, string InColorCode, string InX, string InY)
{
MgMap TheMapHandle = null;
MgResourceService TheResourceService = null;
MgFeatureService TheFeatureService = null;
string SessionString = "";
TheMapHandle = (MgMap)Session["TheMapHandle"];
TheResourceService = (MgResourceService)Session["TheResourceService"];
TheFeatureService = (MgFeatureService)Session["TheFeatureService"];
SessionString = Session["SessionString"].ToString();
//// Create a temporary feature source to draw the lines on
//// Create a feature class definition for the new feature
//// source
MgClassDefinition TheClassDefinition = new MgClassDefinition();
TheClassDefinition.SetName("Points");
TheClassDefinition.SetDescription("Redline Points");
string GeomPropName = "SHPGEOM";
TheClassDefinition.SetDefaultGeometryPropertyName(GeomPropName);
MgDataPropertyDefinition IdentityProperty = new MgDataPropertyDefinition("KEY");
IdentityProperty.Name = "KEY";
IdentityProperty.Description = "KEY Desc";
IdentityProperty.SetDataType(MgPropertyType.Int32);
IdentityProperty.SetAutoGeneration(true);
IdentityProperty.SetReadOnly(true);
TheClassDefinition.GetIdentityProperties().Add(IdentityProperty);
TheClassDefinition.GetProperties().Add(IdentityProperty);
MgDataPropertyDefinition NameProperty = new MgDataPropertyDefinition("NAME");
NameProperty.Name = "NAME";
NameProperty.Description = "NAME Desc";
NameProperty.SetDataType(MgPropertyType.String);
TheClassDefinition.GetProperties().Add(NameProperty);
MgGeometricPropertyDefinition GeometryProperty = new MgGeometricPropertyDefinition(GeomPropName);
GeometryProperty.SetGeometryTypes(MgFeatureGeometricType.Surface);
GeometryProperty.SetSpatialContextAssociation("spatial context");
TheClassDefinition.GetProperties().Add(GeometryProperty);
MgFeatureSchema TheMgFeatureSchema = new MgFeatureSchema("SHP_Schema", "Point schema");
TheMgFeatureSchema.GetClasses().Add(TheClassDefinition);
string TheFeatureSourceresourcestring = "Session:" + SessionString + "//Points.FeatureSource";
MgResourceIdentifier TheMgResourceIdentifierFeatureSource = new MgResourceIdentifier(TheFeatureSourceresourcestring);
string wkt = TheMapHandle.GetMapSRS();
MgCreateSdfParams TheMgCreateSdfParams = new MgCreateSdfParams("spatial context", wkt, TheMgFeatureSchema);
MgFeatureSourceParams TheMgFeatureSourceParams = TheMgCreateSdfParams as MgFeatureSourceParams;
TheFeatureService.CreateFeatureSource(TheMgResourceIdentifierFeatureSource, TheMgFeatureSourceParams);
MgBatchPropertyCollection batchPropertyCollection = new MgBatchPropertyCollection();
MgPropertyCollection PointPropertyCollection = new MgPropertyCollection();
MgStringProperty PointnameProperty = new MgStringProperty("NAME", InPointID);
PointPropertyCollection.Add(PointnameProperty);
MgWktReaderWriter wktReaderWriter = new MgWktReaderWriter();
MgAgfReaderWriter agfReaderWriter = new MgAgfReaderWriter();
MgGeometry ThePointGeom = wktReaderWriter.Read("POINT XY (" + InX + " " + InY + ")");
MgByteReader TheGeomByteReader = agfReaderWriter.Write(ThePointGeom);
MgGeometryProperty ThePointProperty = new MgGeometryProperty("SHPGEOM", TheGeomByteReader);
PointPropertyCollection.Add(ThePointProperty);
batchPropertyCollection.Add(PointPropertyCollection);
// Add the batch property collection to the feature source
MgInsertFeatures TheMgInsertFeaturesCMD = new MgInsertFeatures("Points", batchPropertyCollection);
MgFeatureCommandCollection featureCommandCollection = new MgFeatureCommandCollection();
featureCommandCollection.Add(TheMgInsertFeaturesCMD);
// Execute the "add" commands
TheFeatureService.UpdateFeatures(TheMgResourceIdentifierFeatureSource, featureCommandCollection, false);
string ThePointXMLFileName = "C:\\Temp\\Anand\\RedlineToolPointLayer.LayerDefinition.xml";
XmlDocument TheXmlDocument = new XmlDocument();
TheXmlDocument.Load(ThePointXMLFileName);
XPathNavigator TheXPathNavigator = TheXmlDocument.CreateNavigator();
//XmlNamespaceManager TheXmlNamespaceManager = new XmlNamespaceManager(TheXPathNavigator.NameTable);
//TheXmlNamespaceManager.AddNamespace("bk", "http://www.contoso.com/books");
//foreach (XPathNavigator nav in TheXPathNavigator.Select("//bk:price", TheXmlNamespaceManager))
foreach (XPathNavigator nav in TheXPathNavigator.Select("//VectorLayerDefinition/ResourceId"))
{
this.LabelUserMessage.Text = this.LabelUserMessage.Text + "
" + nav.Value;
nav.SetValue(TheFeatureSourceresourcestring);
}
foreach (XPathNavigator nav in TheXPathNavigator.Select("//VectorLayerDefinition/FeatureName"))
{
this.LabelUserMessage.Text = this.LabelUserMessage.Text + "
" + nav.Value;
nav.SetValue("Points");
}
this.LabelUserMessage.Text = this.LabelUserMessage.Text + "
After Setting ...";
foreach (XPathNavigator nav in TheXPathNavigator.Select("//VectorLayerDefinition/ResourceId"))
{
this.LabelUserMessage.Text = this.LabelUserMessage.Text + "
" + nav.Value;
}
string xmlstring = TheXmlDocument.OuterXml;
this.LabelUserMessage.Text = this.LabelUserMessage.Text + "
xmlstring ..." + xmlstring;
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("UTF-8");
byte[] TheLayerDefData = enc.GetBytes(xmlstring);
MgByteSource TheMgByteSource = new MgByteSource(TheLayerDefData, TheLayerDefData.Length);
TheMgByteSource.SetMimeType(MgMimeType.Xml);
string TheLayerresourcestring = "Session:" + SessionString + "//TemporaryPoints.LayerDefinition";
MgResourceIdentifier TheMgResourceIdentifierLayer = new MgResourceIdentifier(TheLayerresourcestring);
TheResourceService.SetResource(TheMgResourceIdentifierLayer, TheMgByteSource.GetReader(), null);
MgLayer ThePointLayer = new MgLayer(TheMgResourceIdentifierLayer, TheResourceService);
ThePointLayer.SetName("Points");
ThePointLayer.SetVisible(true);
ThePointLayer.SetLegendLabel("Redline Point Layer");
ThePointLayer.SetDisplayInLegend(true);
MgLayerCollection TheMapLayers = TheMapHandle.GetLayers();
if (DoesLayerExist(ThePointLayer.Name, TheMapHandle) == false)
{
TheMapLayers.Insert(0, ThePointLayer);
}
TheMapHandle.Save();
MgFeatureQueryOptions TheQueryOptions = new MgFeatureQueryOptions();
TheQueryOptions.SetFilter("");
MgLayerBase ThePointsLayer = TheMapHandle.GetLayers().GetItem("Points");
MgFeatureReader ThefeatureReader = ThePointsLayer.SelectFeatures(TheQueryOptions);
while (ThefeatureReader.ReadNext())
{
MgByteReader PointGeomByteReader = ThefeatureReader.GetGeometry("SHPGEOM");
MgAgfReaderWriter TheagfReaderWriter = new MgAgfReaderWriter();
MgGeometry CheckPointGeom = TheagfReaderWriter.Read(PointGeomByteReader);
this.LabelUserMessage.Text += "
The POINT Geom = " + CheckPointGeom.Centroid.Coordinate.X + ", " + CheckPointGeom.Centroid.Coordinate.Y;
}
//string JavaScriptCall = "";
//ClientScript.RegisterStartupScript(GetType(), "StartupKey", JavaScriptCall);
string JavaScriptCall = "";
//ClientScript.RegisterStartupScript(GetType(), "StartupKey", JavaScriptCall);
Response.Write(JavaScriptCall);
}