[mapguide-users] Buffer Creation
Kenneth Skovhede, GEOGRAF A/S
ks at geograf.dk
Wed May 20 03:35:23 EDT 2009
You need to use the MgSelection object:
http://mapguide.osgeo.net/files/mapguide/docs/webapi/d7/d8d/class_mg_selection.html
When you have set the correct selection, call save on MgSelection.
Regards, Kenneth Skovhede, GEOGRAF A/S
tejaswi skrev:
> hi
>
> I have to do 2 things in my application
>
> 1. Select a point from a query function that i have called.Once the point is
> selected I have to create a buffer of 1km above this selection point.
> I able to select the point through my programming and create the buffer
> also(I am creating the buffer function by building my own code not using the
> default buffer function).
>
> 2. Now once the buffer is showing on the map.its initial state should be
> selected , i should not select it manually on the map using the
> pointer(mouse).
> I am not able to do this.
>
> I am putting in the code for creating buffer....
>
> public void create_buffer()
> {
>
>
> siteConnection = new MgSiteConnection();
> userInfo = new MgUserInformation(mgsessionid);
>
> siteConnection.Open(userInfo);
> featureService =
> (MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService);
> resourceService =
> (MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService);
>
> mapDefinition = "Library://DELHI/Maps/DelhiMap.MapDefinition";
> resId = new MgResourceIdentifier(mapDefinition);
> mapName = resId.GetName();
> map.Open(resourceService, mapName);
> Lays = map.GetLayers();
>
> //Response.Write(resourceService.GetResourceContent(resId));
> //map.Save(resourceService);
>
>
> /////////----------------------------------------------------------------------
> if (ddl_buffer.Text == "1Km")
> {
> bufferRingSize = 0.003;//0.01;
> }
> if (ddl_buffer.Text == "3Km")
> {
> bufferRingSize = 0.007;//0.02;
> }
> if (ddl_buffer.Text == "5Km")
> {
> bufferRingSize = 0.015;//0.04;
> }
> //LayerName = ddl_layers.Text;
>
>
>
> //Response.Write(bufferRingSize);
> try
> {
> //btn_select_within.Visible = true;
> //btn_buffer_report.Visible = true;
> //lblERR.Visible = false;
> MgSelection selection = new MgSelection(map);
> //Response.Write(selection.GetLayers().GetCount());
> selection.Open(resourceService, mapName);
> LayerName = selection.GetLayers().GetItem(0).GetName();
> layer = LayerByName(map, LayerName);
> //Response.Write(layer.GetName());
> string LayerClassName = layer.GetFeatureClassName();
>
> string selectionString = "";
> selectionString = selection.GenerateFilter(layer,
> LayerClassName);
> string LayerFeatureId = layer.GetFeatureSourceId();
> MgResourceIdentifier LayerFeatureResource = new
> MgResourceIdentifier(LayerFeatureId);
> queryOptions = new MgFeatureQueryOptions();
> queryOptions.SetFilter(selectionString);
> featureReader =
> featureService.SelectFeatures(LayerFeatureResource, LayerClassName,
> queryOptions);
>
> if (selectionString != "")
> {
> //Response.Write("Selected Feature-->" + selectionString);
> bufferRingCount = 3.0;
> string mapWktSrs = map.GetMapSRS();
> MgAgfReaderWriter agfReaderWriter = new MgAgfReaderWriter();
> MgWktReaderWriter wktReaderWriter = new MgWktReaderWriter();
> MgCoordinateSystemFactory coordSysFactory = new
> MgCoordinateSystemFactory();
> MgCoordinateSystem srs = coordSysFactory.Create(mapWktSrs);
> MgCoordinateSystemMeasure srsMeasure = srs.GetMeasure();
> MgResourceIdentifier bufferFeatureResId;
> MgLayerBase bufferLayer;
> // Check for a buffer layer. If it exists, delete
> // the current features.
> // If it does not exist, create a feature source and
> // a layer to hold the buffer.
> MgLayerBase bufferLayer1 = LayerByName(map, "Buffer");
> if (bufferLayer1 == null)//try
> {
> bufferFeatureResId = new MgResourceIdentifier("Session:"
> + mgsessionid + "//Buffer.FeatureSource");
> CreateBufferFeatureSource(featureService, mapWktSrs,
> bufferFeatureResId);
> bufferLayer = CreateBufferLayer(resourceService,
> bufferFeatureResId, mgsessionid);
> map.GetLayers().Insert(0, bufferLayer);
> bufferLayer.ForceRefresh();
> //Response.Write("<script type=text/javascript>");
>
> //Response.Write("parent.parent.mapFrame.Refresh();");
> //Response.Write("</script>");
> map.Save(resourceService);
> //bufferLayer = map.GetLayers().GetItem("Buffer");
> //bufferFeatureResId = new
> MgResourceIdentifier(bufferLayer.GetFeatureSourceId());
> //MgFeatureCommandCollection commands = new
> MgFeatureCommandCollection();
> //commands.Add(new MgDeleteFeatures("BufferClass", "ID
> like '%'"));
> //featureService.UpdateFeatures(bufferFeatureResId,
> commands, false);
> //map.Save(resourceService);
> }
> else//catch (MgObjectNotFoundException e)
> {
> RemoveLayer(map, "Buffer");
> // When an MgObjectNotFoundException is thrown, the
> layer
> // does not exist and must be created.
> bufferFeatureResId = new MgResourceIdentifier("Session:"
> + mgsessionid + "//Buffer.FeatureSource");
> CreateBufferFeatureSource(featureService, mapWktSrs,
> bufferFeatureResId);
> bufferLayer = CreateBufferLayer(resourceService,
> bufferFeatureResId, mgsessionid);
> map.GetLayers().Insert(0, bufferLayer);
> bufferLayer.ForceRefresh();
> Response.Write("<script type=text/javascript>");
>
> Response.Write("parent.parent.mapFrame.Refresh();");
> Response.Write("</script>");
> map.Save(resourceService);
> }
> Double srsDist =
> srs.ConvertMetersToCoordinateSystemUnits(bufferRingSize);
>
> //--------
> ///////////----------------------------------------------------------
> MgGeometry featureGeometry;
> MgLayerCollection selectedLayers = map.GetLayers();
> for (int i = 0; i < selectedLayers.GetCount(); i++)
> {
> // Only check selected features in the Parcels layer.
>
> layer = selectedLayers.GetItem(i);
> //
>
> if (layer.GetName() == LayerName)
> {
> //Response.Write(layer.GetName());
> // Create a filter containing the IDs of the
> selected features on this layer
>
> String layerClassName = layer.GetFeatureClassName();
> selectionString = selection.GenerateFilter(layer,
> layerClassName);
>
> // Get the feature resource for the selected layer
>
> String layerFeatureId = layer.GetFeatureSourceId();
> MgResourceIdentifier layerFeatureResource = new
> MgResourceIdentifier(layerFeatureId);
>
> // Apply the filter to the feature resource for the
> selected layer. This returns
> // an MgFeatureReader of all the selected features.
>
> queryOptions.SetFilter(selectionString);
> featureReader =
> featureService.SelectFeatures(layerFeatureResource, layerClassName,
> queryOptions);
>
> // Process each item in the MgFeatureReader. Get the
> // geometries from all the selected features and
> // merge them into a single geometry.
>
> MgGeometryCollection inputGeometries = new
> MgGeometryCollection();
> while (featureReader.ReadNext())
> {
> //SHPGEOM
> MgByteReader featureGeometryData =
> featureReader.GetGeometry("Geometry");
> featureGeometry =
> agfReaderWriter.Read(featureGeometryData);
>
> inputGeometries.Add(featureGeometry);
> }
> MgGeometryFactory geometryFactory = new
> MgGeometryFactory();
> MgMultiGeometry mergedGeometries =
> geometryFactory.CreateMultiGeometry(inputGeometries);
>
> // Add buffer features to the temporary feature
> source.
> // Create multiple concentric buffers to show area.
> // If the stylization for the layer draws the
> features
> // partially transparent, the concentric rings will
> be
> // progressively darker towards the center.
> // The stylization is set in the layer template
> file, which
> // is used in function CreateBufferLayer().
>
> MgFeatureCommandCollection commands = new
> MgFeatureCommandCollection();
> for (int bufferRing = 0; bufferRing <
> bufferRingCount; bufferRing++)
> {
>
> //Double bufferDist =
> srs.ConvertMetersToCoordinateSystemUnits(bufferRingSize * (bufferRing + 1));
> Double bufferDist = bufferRingSize *(bufferRing
> + 1);
>
> bufferGeometry =
> mergedGeometries.Buffer(bufferDist, srsMeasure);
>
> MgPropertyCollection properties = new
> MgPropertyCollection();
> properties.Add(new
> MgGeometryProperty("BufferGeometry",
> agfReaderWriter.Write(bufferGeometry)));
> //--
> //MgByteReader featureGeometryData =
> featureReader.GetGeometry("Geometry");
> //featureGeometry =
> agfReaderWriter.Read(featureGeometryData);
>
> //==
> commands.Add(new MgInsertFeatures("BufferClass",
> properties));
> }
> MgPropertyCollection results =
> featureService.UpdateFeatures(bufferFeatureResId, commands, false);
>
> bufferLayer.SetVisible(true);
> bufferLayer.ForceRefresh();
> bufferLayer.SetSelectable(true);
>
> bufferLayer.SetDisplayInLegend(true);
> Response.Write("<script type=text/javascript>");
>
> Response.Write("parent.parent.mapFrame.Refresh();");
> Response.Write("</script>");
>
> map.Save(resourceService);
> }
> }
>
>
> }
> }
> catch (NullReferenceException f)
> {
> Response.Write("No selection");
> //btn_select_within.Visible = false;
> //btn_buffer_report.Visible = false;
> //lblERR.Visible = true;
> //lblERR.Text = "Select Parcel On Map";
> }
> }
>
>
>
>
>
>
> public void CreateBufferFeatureSource(MgFeatureService featureService,
> String wkt, MgResourceIdentifier bufferFeatureResId)
> {
> 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);
>
> MgGeometricPropertyDefinition polygonProperty = new
> MgGeometricPropertyDefinition("BufferGeometry");
> polygonProperty.SetGeometryTypes(MgFeatureGeometricType.Surface);
> polygonProperty.SetHasElevation(false);
> polygonProperty.SetHasMeasure(false);
> polygonProperty.SetReadOnly(false);
> polygonProperty.SetSpatialContextAssociation("defaultSrs");
> properties.Add(polygonProperty);
> MgPropertyDefinitionCollection idProperties =
> bufferClass.GetIdentityProperties();
> idProperties.Add(idProperty);
> bufferClass.SetDefaultGeometryPropertyName("BufferGeometry");
> MgFeatureSchema bufferSchema = new
> MgFeatureSchema("BufferLayerSchema", "temporary schema to hold a buffer");
> bufferSchema.GetClasses().Add(bufferClass);
> MgCreateSdfParams sdfParams = new MgCreateSdfParams("defaultSrs",
> wkt, bufferSchema);
> featureService.CreateFeatureSource(bufferFeatureResId, sdfParams);
> }
>
>
>
>
>
>
> public MgLayer CreateBufferLayer(MgResourceService resourceService,
> MgResourceIdentifier bufferFeatureResId, String sessionId)
> {
> // Load the layer definition template into
> // a DOM object, find the "ResourceId" element, and
> // modify its content to reference the temporary
> // feature source.
> XmlDocument doc = new XmlDocument();
> doc.PreserveWhitespace = false;
> doc.Load(Request.ServerVariables["APPL_PHYSICAL_PATH"] +
> "bufferlayerdefinition.xml");
> XmlNode featureSourceNode =
> doc.GetElementsByTagName("ResourceId").Item(0);
> XmlNode resContent =
> doc.CreateTextNode(bufferFeatureResId.ToString());
> featureSourceNode.AppendChild(resContent);
> //featureSourceNode.nodeValue = bufferFeatureResId.ToString();
> // Get the updated layer definition from the DOM object
> // and save it to the session repository using the
> // ResourceService object.
> MemoryStream xmlStream = new MemoryStream();
> doc.Save(xmlStream);//saveXML();
> byte[] layerDefinition = xmlStream.ToArray();
> Encoding utf8 = Encoding.UTF8;
> String layerDefStr = new String(utf8.GetChars(layerDefinition));
> layerDefinition = new byte[layerDefStr.Length - 1];
> int byteCount = utf8.GetBytes(layerDefStr, 1, layerDefStr.Length -
> 1, layerDefinition, 0);
> MgByteSource byteSource = new MgByteSource(layerDefinition,
> layerDefinition.Length);
> byteSource.SetMimeType(MgMimeType.Xml);
>
> MgResourceIdentifier tempLayerResId = new
> MgResourceIdentifier("Session:" + sessionId + "//Buffer.LayerDefinition");
>
> //Response.Write(resourceService.ResourceExists(tempLayerResId));//--Error
> "NOT IMPLEMENTED"
>
> resourceService.SetResource(tempLayerResId, byteSource.GetReader(),
> null);
>
> // Create an MgLayer object based on the new layer definition
> // and return it to the caller.
> MgLayer bufferLayer = new MgLayer(tempLayerResId, resourceService);
> bufferLayer.SetName("Buffer");
> bufferLayer.SetLegendLabel("Buffer");
> bufferLayer.SetDisplayInLegend(true);
> bufferLayer.SetSelectable(false);
> bufferLayer.SetVisible(true);
> Response.Write("<script type=text/javascript>");
>
> Response.Write("parent.parent.mapFrame.Refresh();");
> Response.Write("</script>");
> return bufferLayer;
> }
>
>
>
> Kindly suggest me wat to do..
>
> Thanking you,
> Regards
>
> Tejaswi.
>
>
More information about the mapguide-users
mailing list