[mapguide-users] Help regarding to set filter for a layer
programmatically using Java
Jai Mansukhani
jai_mansukhani at ipssi.com
Thu Aug 13 02:10:12 EDT 2009
Hi,
I would like thank you all for your kind help and time. I am also sorry
for replying to the so late. I am posting the entire java code that i
used.
Thanks
Jai Mansukhani
__________________________
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="org.osgeo.mapguide.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.Object.*"%>
<%@ include file="common/common.jsp" %>
<%@ page import="javax.xml.parsers.*"%>
<%@ page import="org.xml.sax.*"%>
<%@ page import="java.io.StringReader"%>
<%@ page import="org.w3c.dom.*"%>
<%@ page import="javax.xml.transform.*"%>
<%@ page import="javax.xml.transform.dom.*"%>
<%@ page import="javax.xml.transform.stream.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.sql.Timestamp"%>
<%@page import="java.sql.Time"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<%
String sessionId = request.getParameter("SESSION");
String mapName = request.getParameter("MAPNAME");
try {
// Initialize the Web Extensions and connect to the Server using
// the Web Extensions session identifier stored in PHP
// session state.
MapGuideJavaApi.MgInitializeWebTier(webconfigFilePath);
// Get the user information using the session id,
// and set up a connection to the site server.
MgUserInformation userInfo = new MgUserInformation(sessionId);
MgSiteConnection siteConnection = new MgSiteConnection();
siteConnection.Open(userInfo);
// Get an instance of the required service(s).
MgResourceService resService = (MgResourceService)
siteConnection.CreateService(MgServiceType.ResourceService);
MgMap map = new MgMap();
System.out.println("layers ");
map.Open(resService, mapName);
MgLayerCollection layerCollection = map.GetLayers();
MgResourceIdentifier layerResourceId = new
MgResourceIdentifier("Library://Samples/Sheboygan/Layers/filter_test.LayerDefinition");//layer.GetLayerDefinition();
MgByteReader mdReader = resService.GetResourceContent(layerResourceId);
String mdXml = mdReader.ToString();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document mdDomDoc = builder.parse(new InputSource(new
StringReader(mdXml)));
NodeList nodeList = mdDomDoc.getElementsByTagName("Filter");
int size = nodeList.getLength();
Node node = nodeList.item(0);
Element e = (Element) node;
String layerDefinition = "";
try {
DOMSource domSource = new DOMSource(mdDomDoc);
java.io.StringWriter writer = new java.io.StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
layerDefinition = writer.toString();
} catch (TransformerException ex) {
ex.printStackTrace();
}
MgLayerCollection newlayerCollection = map.GetLayers();
MgByteSource byteSource = new
MgByteSource(layerDefinition.getBytes(),layerDefinition.length());
byteSource.SetMimeType( MgMimeType.Xml);
MgResourceIdentifier resId = new
MgResourceIdentifier("Session:"+sessionId+"//Samples/Sheboygan/Layers/new_filter_test.LayerDefinition");
resService.SetResource(resId,byteSource.GetReader(),null);
MgLayer newLayer = new MgLayer(resId,resService);
newLayer.SetName("New Filter Layer");
newLayer.SetVisible(true);
newLayer.SetDisplayInLegend(true);
newLayer.SetLegendLabel("New Filter Layer");
newlayerCollection.Insert(0,newLayer);
map.Save(resService);
}
catch ( MgException e){
System.out.println(e.GetDetails());
e.printStackTrace();
}
%>
<body onload="parent.Refresh();">
</body>
</html>
More information about the mapguide-users
mailing list