[mapguide-users] Draw Line ASP .NET...
Jamo181
adam.jamison at portbris.com.au
Wed Nov 12 00:57:56 EST 2008
Ok hopefully this will be simple enough.
I'm attempting to draw a line using some ASP code.
I have got as far as being able to draw the line show the layer in the
legend, check the feature source to make sure that a property had been
written to the feature view the geometry in the preview tool ... and
everything seems to be in order....
The only thing that isn't happening is once I've completed the task it
doesn't show me the line :(
perhaps I'm missing something in adding it to the map or refreshing the map
....
the legend works... after a map refresh.
<%@ Page language="c#" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<%@ Import Namespace="System.Text" %>
<!-- #Include File="common.aspx -->
<script runat="server">
String x0 = "";
String y0 = "";
String x1 = "";
String y1 = "";
String mapName = "";
String sessionId = "";
String myLayerName = "Redline";
</script>
<%
Response.Charset = "utf-8";
InitializeWebTier();
GetRequestParameters();
MgUserInformation cred = new MgUserInformation(sessionId);
//connect to the site and get a feature service and a resource service
instances
MgSiteConnection site = new MgSiteConnection();
site.Open(cred);
MgResourceService resourceSrvc =
site.CreateService(MgServiceType.ResourceService) as MgResourceService;
//MgResourceService resourceSrvc =
site.CreateService(MgServiceType.ResourceService);
MgFeatureService featureSrvc =
site.CreateService(MgServiceType.FeatureService) as MgFeatureService;
//MgFeatureService featureSrvc =
site.CreateService(MgServiceType.FeatureService);
//Create a temporary map runtime object, locate the layer
MgMap map = new MgMap();
map.Open(resourceSrvc, mapName);
String dataSource = "Session:" + sessionId + "//" + myLayerName +
".FeatureSource";
MgResourceIdentifier dataSourceId = new
MgResourceIdentifier(dataSource);
if (DoesLayerExist(myLayerName, map) == false)
{
//create Feature Source
MgClassDefinition myClassDef = new MgClassDefinition();
myClassDef.SetName(myLayerName);
myClassDef.SetDescription(myLayerName + " Feature Source");
//TODO : make SHPGEOM a Variable...
myClassDef.SetDefaultGeometryPropertyName("SHPGEOM");
//Set KEY Property
MgDataPropertyDefinition prop = new MgDataPropertyDefinition("KEY");
prop.SetDataType(MgPropertyType.Int32);
prop.SetAutoGeneration(true);
prop.SetReadOnly(true);
myClassDef.GetIdentityProperties().Add(prop);
myClassDef.GetProperties().Add(prop);
//Set ID Property
prop = new MgDataPropertyDefinition("ID");
prop.SetDataType(MgPropertyType.Int32);
myClassDef.GetProperties().Add(prop);
//Set geometry property
MgGeometricPropertyDefinition geomProp = new
MgGeometricPropertyDefinition("SHPGEOM");
geomProp.SetGeometryTypes(MgFeatureGeometricType.Curve);
myClassDef.GetProperties().Add(geomProp);
//Create the schema
MgFeatureSchema schema = new MgFeatureSchema(myLayerName + "Schema",
myLayerName + "SchemaDesc");
schema.GetClasses().Add(myClassDef);
MgCreateSdfParams sdfParams = new MgCreateSdfParams("MGA-56 (GDA94 /
MGA zone 56)", map.GetMapSRS(), schema);
featureSrvc.CreateFeatureSource(dataSourceId, sdfParams);
//create layer
String layerDef = "Session:" + sessionId + "//" + myLayerName +
".LayerDefinition";
MgResourceIdentifier layerDefId = new
MgResourceIdentifier(layerDef);
MgByteReader layerDefContent = BuildLayerDefinitionContent(layerDef,
myLayerName, "", "5","F000F");
resourceSrvc.SetResource(layerDefId, layerDefContent, null);
MgLayer myLayer = new MgLayer(layerDefId, resourceSrvc);
myLayer.SetName(myLayerName);
myLayer.SetLegendLabel(myLayerName);
myLayer.SetDisplayInLegend(true);
myLayer.SetSelectable(true);
map.GetLayers().Insert(0, myLayer);
//map.GetLayers().Add(myLayer);
myLayer.ForceRefresh();
map.Save(resourceSrvc);
}
MgInsertFeatures insertFeatures = new MgInsertFeatures(myLayerName,
makeLine("1", x0, y0, x1, y1));
MgFeatureCommandCollection commands = new
MgFeatureCommandCollection();
commands.Add(insertFeatures);
int commandIndex = commands.IndexOf(insertFeatures);
MgPropertyCollection insertResults;
insertResults = featureSrvc.UpdateFeatures(dataSourceId, commands,
false);
MgLayer myLayer1 = map.GetLayers().GetItem(myLayerName) as MgLayer;
myLayer1.ForceRefresh();
//commands.Add(new MgInsertFeatures(myLayerName,makeLine("1", x0,
y0, x1, y1)));
//map.Save(resourceSrvc);
Response.Write("Datasource = " + dataSource);
Response.Write("<BR>");
Response.Write("Layer Name = " +
map.GetLayers().GetItem(myLayerName).GetLegendLabel());
Response.Write("<BR>");
Response.Write("FeatureClass Name = " +
map.GetLayers().GetItem(myLayerName).GetFeatureClassName());
Response.Write("<BR>");
Response.Write("<BR>");
//Response.Write(map.MapSRS);
%>
<script runat=server>
void GetRequestParameters()
{
if (Request.HttpMethod == "POST")
GetParameters(Request.Form);
else
GetParameters(Request.QueryString);
}
void GetParameters(NameValueCollection parameters)
{
mapName = GetParameter(parameters, "MAPNAME");
sessionId = GetParameter(parameters, "SESSION");
x0 = GetParameter(parameters, "x0");
y0 = GetParameter(parameters, "y0");
x1 = GetParameter(parameters, "x1");
y1 = GetParameter(parameters, "y1");
}
MgPropertyCollection makeLine(String Name, String x0, String y0, String x1,
String y1)
{
MgPropertyCollection PropertyCollection = new MgPropertyCollection();
MgInt32Property nameProperty = new MgInt32Property("ID", 1);
PropertyCollection.Add(nameProperty);
MgWktReaderWriter wktReaderWriter = new MgWktReaderWriter();
MgAgfReaderWriter agfReaderWriter = new MgAgfReaderWriter();
MgGeometry geometry = wktReaderWriter.Read("LINESTRING XY (" + x0 + " "
+ y0 + "," + x1 + " " + y1 + ")") as MgLineString;
//MgGeometry geometry = wktReaderWriter.Read("LINESTRING(3 4,10 50,20
25)") as MgLineString;
MgByteReader geometryByteReader = agfReaderWriter.Write(geometry);
MgGeometryProperty geometryProperty = new MgGeometryProperty("SHPGEOM",
geometryByteReader);
PropertyCollection.Add(geometryProperty);
return PropertyCollection;
}
Boolean DoesLayerExist(String LayerName, MgMap Map)
{
MgLayerCollection layers = Map.GetLayers();
return (layers.Contains(LayerName));
}
MgByteReader BuildLayerDefinitionContent(String ResourceId, String
FeatureName, String ToolTip, String Thickness, String LineColor)
{
String layerTempl = LoadTemplate(Request,
"../viewerfiles/linelayerdef.templ");
//String xtrans = String.Format("{0:x2}", (255 * foretrans / 100));
String[] vals = {
ResourceId,
FeatureName,
"SHPGEOM",
ToolTip,
Thickness,
LineColor
};
layerTempl = Substitute(layerTempl, vals);
byte[] bytes = Encoding.UTF8.GetBytes(layerTempl);
MgByteSource src = new MgByteSource(bytes, bytes.Length);
return src.GetReader();
}
</script>
--
View this message in context: http://www.nabble.com/Draw-Line-ASP-.NET...-tp20454706p20454706.html
Sent from the MapGuide Users mailing list archive at Nabble.com.
More information about the mapguide-users
mailing list