[mapguide-users] help

saloua wakrim wakrimsaloua.wakrim at gmail.com
Mon Apr 27 09:26:49 EDT 2009


i try the following code for displaying layer wich contains line in map, so
i digitize a line, create temporary feature for this line, create a layer
wich contains it and add it to the map. it doesn't give me some error but  i
can see the line in the map (i can draw it). Please help me. the code is
following:



String dataSource = "Session:" + sessionid1 + "//" + myLayerName +
".FeatureSource";

MgResourceIdentifier dataSourceId = new MgResourceIdentifier(dataSource);

MgFeatureService featureSrvc = (MgFeatureService
)siteConnection.CreateService(MgServiceType.FeatureService);

if (DoesLayerExist(myLayerName, map) == false)

{



//create Feature Source

MgClassDefinition myClassDef = new MgClassDefinition();

myClassDef.SetName(myLayerName);

myClassDef.SetDescription(myLayerName + " Feature Source");

myClassDef.SetDefaultGeometryPropertyName("SHPGEOM");

MgDataPropertyDefinition prop = new MgDataPropertyDefinition("KEY");

prop.SetDataType(MgPropertyType.Int32);

prop.SetAutoGeneration(true);

prop.SetReadOnly(true);

myClassDef.GetIdentityProperties().Add(prop);

myClassDef.GetProperties().Add(prop);

prop = new MgDataPropertyDefinition("ID");

prop.SetDataType(MgPropertyType.Int32);

myClassDef.GetProperties().Add(prop);

MgGeometricPropertyDefinition geomProp = new MgGeometricPropertyDefinition(
"SHPGEOM");

geomProp.SetGeometryTypes(MgFeatureGeometricType.Curve);

myClassDef.GetProperties().Add(geomProp);

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:" + sessionid1 + "//" + myLayerName +
".LayerDefinition";

MgResourceIdentifier layerDefId = new MgResourceIdentifier(
"Library://essaicalque/Redline.LayerDefinition");





MgByteSource content = new MgByteSource(@"C:\Program
Files\MapGuideOpenSource\WebServerExtensions\www\essaiselect2\Layerdef.xml"
);

resourceSrvc.SetResource(layerDefId, content.GetReader(), null);

 //code enlev‚ d'ici: ajouter layer resource … la carte

//il faut ajouter l'autre code: add layer resource to map

//DOMDocument domdoc = new DOMdocument();

MgLayerresult=add_layer_resource_to_map(layerDefId,resourceSrvc,myLayerName,myLayerName,map);


 }

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);



<script runat=server>

Boolean result;

MgPropertyCollection makeLine(String Name, Double x0, Double y0, Double x1,
Double 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();

result = (layers.Contains(myLayerName));

return result;

}

MgLayer add_layer_resource_to_map(MgResourceIdentifier layerresourceid,
MgResourceService rs, String layername, String layerlegendlabel, MgMap map)

{

MgLayer myLayer = new MgLayer(layerresourceid, rs);

myLayer.SetName(layername);

myLayer.SetLegendLabel(layerlegendlabel);

myLayer.SetDisplayInLegend(true);

myLayer.SetSelectable(true);



if (!map.GetLayers().Contains(layername))

{

map.GetLayers().Insert(0, myLayer);

}

myLayer.SetVisible(true);

myLayer.ForceRefresh();

map.Save(resourceSrvc);

return myLayer;

}

 </script>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapguide-users/attachments/20090427/de21e96a/attachment.html


More information about the mapguide-users mailing list