<%-- -Copyright (C) 2004-2006 Autodesk, Inc. -This library is free software; you can redistribute it and/or -modify it under the terms of version 2.1 of the GNU Lesser -General Public License as published by the Free Software Foundation. -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Lesser General Public License for more details. -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA --%> <%@ page import="org.osgeo.mapguide.*" %> <%@ page import="java.util.*" %> <%@ page import="java.io.*" %> <%@ page import="java.text.*" %> <%@ page import="javax.servlet.jsp.*" %> <%@ page import="javax.servlet.http.*" %> <%@ page import="org.json.*" %> <%@ include file ="property.jsp" %> <%@ include file ="feature.jsp" %> <%! public class Query { Map args = null; private MgSiteConnection site = null; private String[] numOperators = null; private String[] numExpressions = null; private String[] strOperators = null; private String[] strExpressions = null; public Query(Map incomingArgs) throws MgException { this.args = incomingArgs; this.site = new MgSiteConnection(); this.site.Open(new MgUserInformation(this.args.get("SESSION"))); this.numOperators = new String[] {"Equal to", "Not equal to", "Greater than", "Greater than or equal to", "Less than", "Less than or equal to"}; this.numExpressions = new String[] {" = %s", " != %s", " > %s", " >= %s", " < %s", " <= %s"}; this.strOperators = new String[] {"Begins with", "Contains", "Equal to"}; this.strExpressions = new String[] {" like '%s%%'", " like '%%%s%%'", " = '%s'"}; } public String getNumOp() { JSONArray jsonArray = new JSONArray(); for(int i=0;i<6;i++) { jsonArray.put(numOperators[i]); } return jsonArray.toString(); } public String getStrOp() { JSONArray jsonArray = new JSONArray(); for(int i=0;i<3;i++) { jsonArray.put(strOperators[i]); } return jsonArray.toString(); } public ArrayList GetMapLayerNames() throws MgException { MgMap map = new MgMap(this.site); map.Open(this.args.get("MAPNAME")); MgLayerCollection layers = map.GetLayers(); ArrayList layerNames = new ArrayList(); for(int i=0; i GetLayerProperties() throws MgException { ArrayList properties = new ArrayList(); MgMap map = new MgMap(this.site); map.Open(this.args.get("MAPNAME")); MgLayer layer = (MgLayer) map.GetLayers().GetItem(this.args.get("LAYERNAME")); MgClassDefinition classDef = layer.GetClassDefinition(); for(int i=0; i Execute() throws MgException { ArrayList result = new ArrayList(); MgMap map = new MgMap(this.site); map.Open(this.args.get("MAPNAME")); MgLayer layer = (MgLayer) map.GetLayers().GetItem(this.args.get("LAYERNAME")); MgFeatureService featureService = (MgFeatureService)this.site.CreateService(MgServiceType.FeatureService); MgResourceIdentifier resId = new MgResourceIdentifier(layer.GetFeatureSourceId()); String featureGeometry = layer.GetFeatureGeometryName(); // Initialize the coordinate system transform MgClassDefinition classDef = layer.GetClassDefinition(); MgGeometricPropertyDefinition geomProp = (MgGeometricPropertyDefinition) classDef.GetProperties().GetItem(featureGeometry); String spatialContext = geomProp.GetSpatialContextAssociation(); MgCoordinateSystemTransform csTransform = null; MgCoordinateSystemTransform csInverseTransform = null; MgCoordinateSystemFactory coordSysFactory = new MgCoordinateSystemFactory(); MgSpatialContextReader scReader = featureService.GetSpatialContexts(resId, false); while(scReader.ReadNext() && csTransform==null) { if(spatialContext.equals(scReader.GetName())) { MgCoordinateSystem source = coordSysFactory.Create(scReader.GetCoordinateSystemWkt()); MgCoordinateSystem target = coordSysFactory.Create(map.GetMapSRS()); csTransform = coordSysFactory.GetTransform(source, target); csInverseTransform = coordSysFactory.GetTransform(target, source); } } scReader.Close(); // Execute the query int queryMax = Integer.parseInt(this.args.get("QUERYMAX").trim()); MgFeatureQueryOptions queryOptions = new MgFeatureQueryOptions(); if(this.args.get("USEPROPERTYFILTER").equals("true")) { String propertyFilter = this.args.get("PROPERTYNAME"); int operator = Integer.valueOf(this.args.get("OPERATOR")); int count = 0; if(this.args.get("ISSTRING").equals("true")) { propertyFilter = propertyFilter + this.strExpressions[operator].replaceAll("%s", this.args.get("VALUE")); } else { propertyFilter = propertyFilter + this.numExpressions[operator].replaceAll("%s", this.args.get("VALUE")); } queryOptions.SetFilter(propertyFilter); } if(this.args.get("USESPATIALFILTER").equals("true")) { MgPolygon polygon = this.CreatePolygonFromGeomText(this.args.get("GEOMTEXT")); MgGeometry geometry = (MgGeometry) polygon.Transform(csInverseTransform); queryOptions.SetSpatialFilter(featureGeometry, geometry, MgFeatureSpatialOperations.Intersects); } int count = 0; MgAgfReaderWriter geometryReaderWriter = new MgAgfReaderWriter(); MgFeatureReader featureReader = layer.SelectFeatures(queryOptions); String displayValue = null; while(featureReader.ReadNext() && (queryMax <= 0 || count < queryMax)) { MgByteReader byteReader = featureReader.GetGeometry(featureGeometry); MgPoint centerPoint = null; try { MgGeometry geometry = geometryReaderWriter.Read(byteReader); centerPoint = geometry.GetCentroid(); centerPoint = (MgPoint) centerPoint.Transform(csTransform); } catch (MgException ex) //Maybe because of bad geometry { centerPoint = null; } Map idList = new HashMap(this.GetFeatureIdList(featureReader)); int propertyType = featureReader.GetPropertyType(this.args.get("OUTPUTPROPERTY")); switch(propertyType) { case MgPropertyType.Boolean : displayValue = String.valueOf(featureReader.GetBoolean(this.args.get("OUTPUTPROPERTY"))); break; case MgPropertyType.Byte : displayValue = String.valueOf(featureReader.GetByte(this.args.get("OUTPUTPROPERTY"))); break; case MgPropertyType.Single : displayValue = String.valueOf(featureReader.GetSingle(this.args.get("OUTPUTPROPERTY"))); break; case MgPropertyType.Double : displayValue = String.valueOf(featureReader.GetDouble(this.args.get("OUTPUTPROPERTY"))); break; case MgPropertyType.Int16 : displayValue = String.valueOf(featureReader.GetInt16(this.args.get("OUTPUTPROPERTY"))); break; case MgPropertyType.Int32 : displayValue = String.valueOf(featureReader.GetInt32(this.args.get("OUTPUTPROPERTY"))); break; case MgPropertyType.Int64 : displayValue = String.valueOf(featureReader.GetInt64(this.args.get("OUTPUTPROPERTY"))); break; case MgPropertyType.String : displayValue = featureReader.GetString(this.args.get("OUTPUTPROPERTY")); break; case MgPropertyType.DateTime : case MgPropertyType.Null : case MgPropertyType.Blob : case MgPropertyType.Clob : case MgPropertyType.Feature : case MgPropertyType.Geometry : case MgPropertyType.Raster : displayValue = "[unsupported data type]"; break; } result.add(new Feature(displayValue, centerPoint, idList)); count++; } return result; } public String GetSelectionXML() throws MgException, JSONException { MgFeatureService featureService = (MgFeatureService)this.site.CreateService(MgServiceType.FeatureService); MgMap map = new MgMap(this.site); map.Open(this.args.get("MAPNAME")); MgLayer layer = (MgLayer) map.GetLayers().GetItem(this.args.get("LAYERNAME")); String featureClass = layer.GetFeatureClassName(); MgClassDefinition classDef = layer.GetClassDefinition(); MgPropertyCollection properties = new MgPropertyCollection(); MgDataPropertyDefinition dataPropDef = null; JSONObject json = new JSONObject(this.args.get("IDLIST")); Iterator it = json.keys(); while(it.hasNext()) { String key = (String) it.next(); Object value = json.get(key); dataPropDef = (MgDataPropertyDefinition) classDef.GetProperties().GetItem(key); switch(dataPropDef.GetDataType()) { case MgPropertyType.Boolean : properties.Add(new MgBooleanProperty(key, Boolean.valueOf(value.toString()))); break; case MgPropertyType.Byte : properties.Add(new MgByteProperty(key, Byte.valueOf(value.toString()))); break; case MgPropertyType.Single : properties.Add(new MgSingleProperty(key, Float.valueOf(value.toString()))); break; case MgPropertyType.Double : properties.Add(new MgDoubleProperty(key, Double.valueOf(value.toString()))); break; case MgPropertyType.Int16 : properties.Add(new MgInt16Property(key, Short.valueOf(value.toString()))); break; case MgPropertyType.Int32 : properties.Add(new MgInt32Property(key, Integer.valueOf(value.toString()))); break; case MgPropertyType.Int64 : properties.Add(new MgInt64Property(key, Long.valueOf(value.toString()))); break; case MgPropertyType.String : properties.Add(new MgStringProperty(key, value.toString())); break; case MgPropertyType.DateTime : case MgPropertyType.Null : case MgPropertyType.Blob : case MgPropertyType.Clob : case MgPropertyType.Feature : case MgPropertyType.Geometry : case MgPropertyType.Raster : break; } } MgSelection selection = new MgSelection(map); selection.AddFeatureIds(layer, featureClass, properties); return selection.ToXml(); } private Map GetFeatureIdList(MgFeatureReader featureReader) throws MgException { MgClassDefinition classDef = featureReader.GetClassDefinition(); MgPropertyDefinitionCollection idProps = classDef.GetIdentityProperties(); Map idList = new HashMap(); for(int i=0; i