[mapguide-users] Saving a point to map permanently
saloua wakrim
wakrimsaloua.wakrim at gmail.com
Tue Jun 9 09:21:43 EDT 2009
Skipped content of type multipart/alternative-------------- next part --------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class essai : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
-------------- next part --------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="essai.aspx.cs"
Inherits="essai" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<%@ Import Namespace="OSGeo.MapGuide" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>selection</title>
</head>
<body>
<%
String sessionid1;
MgSiteConnection siteConnection;
MgResourceService resourceSrvc;
String mapname;
MgMap map;
mapname = "Shboygan";
String configPath = "C:\\Program Files\\MapGuideOpenSource\\WebServerExtensions\\www\\webconfig.ini";
MapGuideApi.MgInitializeWebTier(configPath);
MgSite site1 = new MgSite();
MgUserInformation userInfo = new MgUserInformation("Administrator", "admin");
siteConnection = new MgSiteConnection();
siteConnection.Open(userInfo);
site1 = siteConnection.GetSite();
sessionid1 = site1.CreateSession();
userInfo.SetMgSessionId(sessionid1);
resourceSrvc = (MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService) as MgResourceService;
MgResourceIdentifier resourceID = new MgResourceIdentifier("Library://Samples/Sheboygan/Maps/Sheboygan.MapDefinition");
MgFeatureService featureService = (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService);
map = new MgMap(siteConnection);
map.Create(resourceSrvc, resourceID, mapname);
map.Save();
map.Open(resourceSrvc, mapname);
string myLayerName = "drawdeuxpoints";
String wkt = map.GetMapSRS();
MgClassDefinition bufferClass = new MgClassDefinition();
bufferClass.SetName("BufferClass");
MgPropertyDefinitionCollection properties = bufferClass.GetProperties();
MgDataPropertyDefinition idProperty = new MgDataPropertyDefinition("ID");
idProperty.SetDataType(MgPropertyType.Int32);
idProperty.SetReadOnly(true);
idProperty.SetNullable(false);
idProperty.SetAutoGeneration(true);
properties.Add(idProperty);
MgPropertyDefinitionCollection idProperties = bufferClass.GetIdentityProperties();
idProperties.Add(idProperty);
MgGeometricPropertyDefinition polygonProperty = new MgGeometricPropertyDefinition("BufferGeometry");
polygonProperty.SetGeometryTypes(MgFeatureGeometricType.Point);
polygonProperty.SetHasElevation(false);
polygonProperty.SetHasMeasure(false);
polygonProperty.SetSpatialContextAssociation("LL84");
properties.Add(polygonProperty);
bufferClass.SetDefaultGeometryPropertyName("BufferGeometry");
MgDataPropertyDefinition labelProperty = new MgDataPropertyDefinition("Label");
labelProperty.SetDataType(MgPropertyType.String);
properties.Add(labelProperty);
MgFeatureSchema bufferSchema = new MgFeatureSchema("BufferLayerSchema", "temporary schema to hold a buffer");
bufferSchema.GetClasses().Add(bufferClass);
//String wkt = "GEOGCS["+"WGS84 Lat/Long's, Degrees, -180 ==> +180"+", DATUM["+"D_WGS_1984"+", SPHEROID["+"World_Geodetic_System_of_1984"+", 6378137, 298.257222932867]], PRIMEM["+"Greenwich"+", 0], UNIT["+"Degree"+", 0.017453292519943295]]";
MgCreateSdfParams sdfParams = new MgCreateSdfParams("LL84", map.GetMapSRS(), bufferSchema);
string featurePath = "Library://Samples/Sheboygan/Data/drawdeuxpoints.FeatureSource";
MgResourceIdentifier bufferFeatureResId = new MgResourceIdentifier(featurePath);
featureService.CreateFeatureSource(bufferFeatureResId, sdfParams);
/*MgPropertyCollection propertyValues = new MgPropertyCollection();
MgPropertyCollection propertyValues2 = new MgPropertyCollection();
MgGeometryFactory factory = new MgGeometryFactory();
propertyValues.Add(new MgGeometryProperty("BufferGeometry", new MgAgfReaderWriter().Write(factory.CreatePoint(factory.CreateCoordinateXY(-87.704986990962839, 43.707520000480347)))));
propertyValues2.Add(new MgGeometryProperty("BufferGeometry", new MgAgfReaderWriter().Write(factory.CreatePoint(factory.CreateCoordinateXY(-87.744986990962839, 43.747520000480347)))));
//à revoir c koi feature source name
propertyValues.Add(new MgStringProperty("Label", "point1"));
propertyValues.Add(new MgStringProperty("Label", "ponit2"));*/
MgInsertFeatures insert = new MgInsertFeatures("BufferLayerSchema:BufferClass", point1());
MgInsertFeatures insert2 = new MgInsertFeatures("BufferLayerSchema:BufferClass", point2());
MgFeatureCommandCollection commands = new MgFeatureCommandCollection();
commands.Add(insert);
commands.Add(insert2);
featureService.UpdateFeatures(bufferFeatureResId, commands, false);
//LayerDefinition
//MgResourceIdentifier templateLayerId = new MgResourceIdentifier("Library://Samples/Sheboygan/Layers/calquepoint.LayerDefinition");
//MgByteReader oldContent = resourceService.GetResourceContent(templateLayerId);
StreamReader sr = File.OpenText("C:\\Program Files\\MapGuideOpenSource\\WebServerExtensions\\www\\point.txt");
String template = sr.ReadToEnd();
//string strXml = oldContent.ToString();
XmlDocument document = new XmlDocument();
document.LoadXml(template);
XmlNode node = document.SelectSingleNode("LayerDefinition/VectorLayerDefinition/ResourceId");
node.InnerText = featurePath;
System.IO.MemoryStream outStream = new System.IO.MemoryStream();
document.Save(outStream);
byte[] newByteXml = outStream.ToArray();
outStream.Close();
string newStrXml = Encoding.UTF8.GetString(newByteXml);
newByteXml = Encoding.UTF8.GetBytes(newStrXml.Substring(1));
MgByteSource source = new MgByteSource(newByteXml, newByteXml.Length);
MgByteReader newContent = source.GetReader();
string layerPath = "Library://Samples/Sheboygan/Layers/drawdeuxpoints.LayerDefinition";
MgResourceIdentifier newLayerId = new MgResourceIdentifier(layerPath);
resourceSrvc.SetResource(newLayerId, newContent, null);
MgLayer layer = new MgLayer(newLayerId, resourceSrvc);
layer.SetDisplayInLegend(true);
layer.SetLegendLabel("BufferClass");
layer.SetName("BufferClass");
layer.SetSelectable(true);
layer.SetVisible(true);
layer.ForceRefresh();
MgLayerCollection layers = map.GetLayers();
layers.Add(layer);
map.Save(resourceSrvc);
mesuredistance(map);
//Response.Write(DoesLayerExist(myLayerName, map).ToString());
MgByteSource content2 = new MgByteSource("C:\\Program Files\\MapGuideOpenSource\\WebServerExtensions\\www\\drawpoint\\mapdefinition.xml");
resourceSrvc.SetResource(resourceID, content2.GetReader(), null);
map.Save(resourceSrvc);
%>
<div id="Div1">
<iframe id="carte" width="1000" height="800" frameborder="0" scrolling="no" src="http://localhost/mapguide/mapviewerajax/?SESSION=<%=sessionid1 %>&WEBLAYOUT=Library%3a%2f%2fSamples%2fSheboygan%2fLayouts%2fSheboyganAsp.WebLayout" name="fenetre"></iframe>
</div>
<br />
<div>
<input type="button" value="rafresh" name="digitligne" onclick="rafresh();"/>
</div>
</body>
<script type="text/javascript">
function rafresh()
{
//carte.GetMapFrame().GetLayers(true,true);
//carte.GetScriptFrame().location.href = "essai.aspx";
frames['fenetre'].location.reload();
carte.GetMapFrame().Refresh();
//carte.ExecuteMapAction(20);
//carte.GetMapFrame().ExecuteMapAction(1);
//carte.GetTaskFrame().location.href = "essai.aspx"
//carte.GetScriptFrame().location.href = "essai.aspx"
//carte.GetMapFrame().ExecuteMapAction(20);
//carte.GetMapFrame().ExecuteMapAction(20);
}
</script>
<script runat=server>
MgGeometry pointa;
MgGeometry pointb;
Double distanceunits;
Double distancemeters;
Boolean DoesLayerExist(String LayerName, MgMap Map)
{
MgLayerCollection layers = Map.GetLayers();
return (layers.Contains(LayerName));
}
MgPropertyCollection point1()
{
MgPropertyCollection propertyValues = new MgPropertyCollection();
//MgPropertyCollection propertyValues2 = new MgPropertyCollection();
MgGeometryFactory factory = new MgGeometryFactory();
pointa = factory.CreatePoint(factory.CreateCoordinateXY(-87.704986990962839, 43.707520000480347));
propertyValues.Add(new MgGeometryProperty("BufferGeometry", new MgAgfReaderWriter().Write(pointa)));
//propertyValues2.Add(new MgGeometryProperty("BufferGeometry", new MgAgfReaderWriter().Write(factory.CreatePoint(factory.CreateCoordinateXY(-87.744986990962839, 43.747520000480347)))));
//à revoir c koi feature source name
propertyValues.Add(new MgStringProperty("Label", "point1"));
//propertyValues.Add(new MgStringProperty("Label", "ponit2"));
return propertyValues;
}
MgPropertyCollection point2()
{
MgPropertyCollection propertyValues2 = new MgPropertyCollection();
MgGeometryFactory factory2 = new MgGeometryFactory();
//propertyValues.Add(new MgGeometryProperty("BufferGeometry", new MgAgfReaderWriter().Write(factory2.CreatePoint(factory.CreateCoordinateXY(-87.704986990962839, 43.707520000480347)))));
pointb = factory2.CreatePoint(factory2.CreateCoordinateXY(-87.744986990962839, 43.747520000480347));
propertyValues2.Add(new MgGeometryProperty("BufferGeometry", new MgAgfReaderWriter().Write(pointb)));
//à revoir c koi feature source name
propertyValues2.Add(new MgStringProperty("Label", "point2"));
//propertyValues.Add(new MgStringProperty("Label", "ponit2"));
return propertyValues2;
}
public Double mesuredistance(MgMap map)
{
String srs=map.GetMapSRS();
MgCoordinateSystemMeasure mesure = new MgCoordinateSystemMeasure(srs);
distanceunits = pointa.Distance(pointb, mesure);
Response.Write(distanceunits.ToString());
//distancemeters = srs.ConvertCoordinateSystemUnitsToMeters(distanceunits);
//Response.Write(distancemeters.ToString());
return distancemeters;
}
</script>
</html>
-------------- next part --------------
<?xml version="1.0" encoding="UTF-8"?>
<LayerDefinition version="1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="LayerDefinition-1.1.0.xsd">
<VectorLayerDefinition>
<ResourceId>%s</ResourceId>
<FeatureName>BufferLayerSchema:BufferClass</FeatureName>
<FeatureNameType>FeatureClass</FeatureNameType>
<Geometry>BufferGeometry</Geometry>
<ToolTip> </ToolTip>
<VectorScaleRange>
<PointTypeStyle>
<DisplayAsText>true</DisplayAsText>
<AllowOverpost>false</AllowOverpost>
<PointRule>
<LegendLabel>point1</LegendLabel>
<PointSymbolization2D>
<Mark>
<Unit>Centimeters</Unit>
<SizeContext>DeviceUnits</SizeContext>
<SizeX>0.3</SizeX>
<SizeY>0.3</SizeY>
<Rotation>0</Rotation>
<Shape>Square</Shape>
<Fill>
<FillPattern>Solid</FillPattern>
<ForegroundColor>FF0000FF</ForegroundColor>
<BackgroundColor>FF000000</BackgroundColor>
</Fill>
</Mark>
</PointSymbolization2D>
</PointRule>
</PointTypeStyle>
</VectorScaleRange>
</VectorLayerDefinition>
</LayerDefinition>
More information about the mapguide-users
mailing list