[mapguide-users] Draw polygon in Mapguide using Asp.net

A H hizperion at hotmail.com
Fri Sep 28 00:12:28 PDT 2012


since i can't find way to edit the vertex using built-in function, i decided
to allow user to draw multiple polygons. the selected polygons can later be
used to be combined, or clear polygons that it overlaps with.


here's the sample code:

<html>

<head>	
	
</head>

<body class="AppFrame" onLoad="InitDocument()">
<%		
		bool mapUpdated = false;
		string returnStat = "";
		
		string mgSessionId = GetParameters()["SESSION"];
		
		Uri MyUrl = Request.Url;
		Response.Write("URL: " + MyUrl.AbsoluteUri+ "<br><br>");
		
		try
		{
	        string selXml = GetParameters()["selXml"];
	        string selCmd = GetParameters()["command"];
			
			Response.Write(selCmd + "<br>");
			Response.Write(selXml);
			
			if(selCmd=="remove")
				ClearPolygon(selXml, mgSessionId);
			if(selCmd=="combine")
				returnStat = CombinePolygon(selXml, mgSessionId);
			if(selCmd=="removeall")
				ClearPolygon(mgSessionId);
			if(selCmd=="combineall")
				returnStat = CombinePolygon(mgSessionId);
			
			if(returnStat=="")
				mapUpdated = true;
		}
		
		catch (MgException mge)
        {
            Response.Write(mge.GetExceptionMessage());
            Response.Write(mge.GetDetails());
        }
%>
</body>



</html>

<script language="c#" runat="server">

void ClearPolygon(string mgSessionId)
{
	Clear("ID > 0", mgSessionId);
}

void ClearPolygon(string selectionXml, string mgSessionId)
{
	MgUserInformation userInfo = new MgUserInformation(mgSessionId);
	MgSiteConnection siteConnection = new MgSiteConnection();
	siteConnection.Open(userInfo);
	
	MgMap map = new MgMap(siteConnection);
	map.Open(WORLD_MAP);
	
	MgLayer polyLayer = GetLayerByName(map, "PolyMarker");
	MgSelection selection = new MgSelection (map, selectionXml);
	Clear(selection.GenerateFilter(polyLayer,"PolyMarker"), mgSessionId);
}

void Clear(string selectionFilter, string mgSessionId)
{		
	try
	{			
		InitializeWebTier ();

		MgUserInformation userInfo = new MgUserInformation(mgSessionId);
		MgSiteConnection siteConnection = new MgSiteConnection();
		siteConnection.Open(userInfo);
		
		MgResourceService resourceService =
siteConnection.CreateService(MgServiceType.ResourceService) as
MgResourceService;
		MgFeatureService featureService =
siteConnection.CreateService(MgServiceType.FeatureService) as
MgFeatureService;
		
		MgResourceIdentifier FeatureSourceId = new MgResourceIdentifier("Session:"
+ mgSessionId  + "//PolyMarker.FeatureSource");
		MgFeatureCommandCollection Commands = new MgFeatureCommandCollection();
		
		MgMap map = new MgMap(siteConnection);
		map.Open(WORLD_MAP);
		
		MgLayer polyLayer = GetLayerByName(map, "PolyMarker");
		
		if (polyLayer != null)
		{
			Response.Write("<br/>" + selectionFilter);
			
			Commands.Add(new MgDeleteFeatures("PolyMarker", selectionFilter));
			
			//featureService.UpdateFeatures(FeatureSourceId, Commands, false);
			ReleaseReader(featureService.UpdateFeatures(FeatureSourceId, Commands,
false), Commands);

			polyLayer.SetVisible(true);
			polyLayer.ForceRefresh();

			map.Save(resourceService);
		}
	}
	
	catch (MgException mge)
	{
		Response.Write(mge.GetExceptionMessage());
		Response.Write(mge.GetDetails());
	}
}

string CombinePolygon(string mgSessionId)
{
	return Combine("ID > 0", mgSessionId);
}

string CombinePolygon(string selectionXml, string mgSessionId)
{
	MgUserInformation userInfo = new MgUserInformation(mgSessionId);
	MgSiteConnection siteConnection = new MgSiteConnection();
	siteConnection.Open(userInfo);
	
	MgMap map = new MgMap(siteConnection);
	map.Open(WORLD_MAP);
	
	MgLayer polyLayer = GetLayerByName(map, "PolyMarker");
	MgSelection selection = new MgSelection (map, selectionXml);
	return Combine(selection.GenerateFilter(polyLayer,"PolyMarker"),
mgSessionId);
}

string Combine(string selectionFilter,string mgSessionId)
{
	bool flag = true;
	DateTime startTime = DateTime.Now;
	
	try
	{
		InitializeWebTier ();

		MgUserInformation userInfo = new MgUserInformation(mgSessionId);
		MgSiteConnection siteConnection = new MgSiteConnection();
		siteConnection.Open(userInfo);
		
		MgResourceService resourceService =
siteConnection.CreateService(MgServiceType.ResourceService) as
MgResourceService;
		MgFeatureService featureService =
siteConnection.CreateService(MgServiceType.FeatureService) as
MgFeatureService;
		
		MgResourceIdentifier FeatureSourceId = new MgResourceIdentifier("Session:"
+ mgSessionId  + "//PolyMarker.FeatureSource");
		MgFeatureCommandCollection Commands = new MgFeatureCommandCollection();
		
		MgMap map = new MgMap(siteConnection);
		map.Open(WORLD_MAP);
		
		MgLayer polyLayer = GetLayerByName(map, "PolyMarker");
		
		if (polyLayer != null)
		{			
			Response.Write("<br/>" + selectionFilter);
			
			MgFeatureQueryOptions selQuery = new MgFeatureQueryOptions();
			selQuery.SetFilter(selectionFilter);
			
			MgFeatureReader featureReader = polyLayer.SelectFeatures(selQuery);
			
			MgGeometryCollection geometries = new MgGeometryCollection();
			MgAgfReaderWriter geometryReaderWriter = new MgAgfReaderWriter();
			MgGeometryFactory geometryFactory = new MgGeometryFactory();
			MgPolygon newPolygon;
							
			while(featureReader.ReadNext())
			{
				MgByteReader temp = featureReader.GetGeometry("GEOM");
				
				MgGeometry tempGeo = geometryReaderWriter.Read(temp);
				
				geometries.Add(tempGeo);
			}
			
			for(int i=0; i<geometries.GetCount(); i++)	// check if geometries
intersects with each other
			{
				bool onceIntersect = false;
				for(int j=0; j<geometries.GetCount(); j++)
				{
					if(i!=j)
						if(geometries.GetItem(i).Intersects(geometries.GetItem(j)))
							onceIntersect = true;
				}
				if(!onceIntersect)
				{
					flag = false;
					return "Not all polygon intersects with each other";
				}
			}
			
			if(flag)
			{
				MgGeometry newGeometry = geometries.GetItem(0);
				
				for(int i=1; i<geometries.GetCount(); i++)	// combine the geometries
				{
					newGeometry = newGeometry.Union(geometries.GetItem(i));
				}

				MgByteReader geom = geometryReaderWriter.Write(newGeometry);
				
				MgPropertyCollection properties = new MgPropertyCollection();
				
				properties.Add(new MgGeometryProperty("GEOM", geom));
				properties.Add(new MgStringProperty("Text", ""));
				
				Commands.Add(new MgDeleteFeatures("PolyMarker", selectionFilter));
				Commands.Add(new MgInsertFeatures("PolyMarker", properties));
				
				//featureService.UpdateFeatures(FeatureSourceId, Commands, false);
				ReleaseReader(featureService.UpdateFeatures(FeatureSourceId, Commands,
false), Commands);
				
				map.Save(resourceService);
				polyLayer.SetVisible(true);
				polyLayer.ForceRefresh();
			}
		}
	}
	
	catch (MgException mge)
	{
		Response.Write(mge.GetExceptionMessage());
		Response.Write(mge.GetDetails());
	}
	
	return "";
}



--
View this message in context: http://osgeo-org.1560.n6.nabble.com/Edit-polygon-tp4183861p5005108.html
Sent from the MapGuide Users mailing list archive at Nabble.com.


More information about the mapguide-users mailing list