The usage of map component |
The Map components provides a series of map related GIS functions, including clearing the map cache, getting the available map name, measuring, querying, zooming, etc. Here we take the SuperMap file workspace as an example to construct a map component, and implement the functions such as querying, outputting, measuring,etc.
Detailed sample codes are shown: %SuperMap iServer_HOME%/samples/code/UseJavaAPI/MapComponentSample .
The Map component can be constructed by the MapContext, and you need to set map service component’s configuration information and the map service provider for a MapContext. As to the service provider settings, two methods are provided for service providers and service provider configuration information settings.
The above figure shows the process of using the SuperMap data to construct a Map component (MapImpl), where the arrows shows the initialization process. Here we follow the solid arrows and the dotted arrows indicate the other method for the service provider setting. Firstly, construct the MapContext through MapConfig and UGCMapProviderSetting. Then initialize the map component (i.e., MapImpl object) through the map component context. The sample code is shown as below:
// Initialize the settings of the SuperMap map service provider. UGCMapProviderSetting ugcPSetting = new UGCMapProviderSetting(); // The path of the SuperMap workspace. String workspacePath = "../../../data/China400/China400.smwu"; if (!new File(workspacePath).exists()) { System.out.println(workspacePath + " doesn't exist, please change the path."); System.exit(0); } ugcPSetting.setWorkspacePath(workspacePath); // The output path of the map image of the map service provider. ugcPSetting.setOutputPath(".\\output"); // The output site of the map image of the map service provider. ugcPSetting.setOutputSite("http://localhost"); ugcPSetting.setName("default"); // The map service provider set, used to initialize the map service component context. List<mapprovidersetting> ugcPSettings = new ArrayList(mapprovidersetting); ugcPSettings.add(ugcPSetting); // Initialize the map service component context. MapContext mapContext = new MapContext(); mapContext.setMapProviderSettings(ugcPSettings); // Create the map service component. MapImpl mapc = new MapImpl(mapContext);
Buffer query is to find the geometry objects on a map whose distance to a specified geometry is within a certain value.
Syntax:
public QueryResult queryByDistance(java.lang.String mapName, Geometry geometry, double distance, QueryParameterSet queryParameterSet)
Parameters:
The result is a QueryResult object containing multiple recordsets, the number of which is identical to that of the query parameters in QueryParameterSet.
Sample code://The query parameter set
// The query parameter set QueryParameterSet queryParameters = new QueryParameterSet(); QueryParameter[] queryLayerParams = new QueryParameter[1]; queryLayerParams[0] = new QueryParameter(); queryLayerParams[0].name = "China_Capital_P@China400"; // Attribute filter condition. Here is set to null queryLayerParams[0].attributeFilter = ""; // The field name returned queryLayerParams[0].fields = new String[] { "AdminName", "AdminCode" }; queryParameters.queryParams = queryLayerParams; // Set the query result that only contains attribute information queryParameters.queryOption = QueryOption.ATTRIBUTE; // Create the reference geometry object, a triangular region, for buffer analysis. Here for a triangular planar object. Point2D p1 = new Point2D(12128888.89, 4628888.89); Point2D p2 = new Point2D(11000000.0, 3500000.0); Point2D p3 = new Point2D(12128888.89, 3500000.0); Point2D[] point2ds = { p1, p2, p3 }; Geometry geometry = Geometry.fromPoint2Ds(point2ds, GeometryType.REGION); // Query all features within 100 of the specified objects on the layer of China_Capital_P@China400 of the map of China QueryResult queryResult = mapc.queryByDistance("China", geometry, 100, queryParameters); mapc.dispose();
Output the map of the specified size by specifying the center and the scale. The default size of the map image is 256 pixels *256 pixels.
Syntax:
public MapImage viewByScale(Point2D center, double scale, MapParameter mapParameter, ImageOutputOption outputOption)
Parameters:
The result is a MapImage object, and the specified output format is PNG binary stream. If the specified output format is an image format, MapImage will contain the URI for the result image, which is stored in the location specified when creating the map component. In this example, the image will be located in . output, i.e., the current folder under the current directory. The sample code is as follows:
Note: The cache path in the service provider configuration would also work. That is to say, there will be cache images in both cache paths.
// Initialize the map service control MapImpl mapc = getMapComponent(); // The center coordinates Point2D center = new Point2D(11000000.0, 3500000.0); // The scale of the map double scale = 0.00000003; // The map parameters. Here the default parameters of China is used. MapParameter mapParameter = mapc.getDefaultMapParameter("China"); // Image output settings. Here the output format is set to PNG. ImageOutputOption outputOption = new ImageOutputOption(); mapParameter.returnType = ReturnType.BINARY; outputOption.format = OutputFormat.PNG; MapImage mapImage = mapc.viewByScale(center, scale, mapParameter, outputOption); // Dispose the resources mapc.dispose();
Area measurement is to measure the enclosed area of a set of points. Users can specify the unit as parameter.
Performs area measurement according to map name, 2D geographic coordinate point, measurement parameter.
Syntax:
public MeasureResult measureArea(java.lang.String mapName, Point2D[] points, MeasureParameter measureParameter)
Parameters:
The result is a MeasureResult object including the value and the unit. If the measurement unit is METER, the unit of the measurement result is square meters.
Point2D p1 = new Point2D(12128888.89, 4628888.89); Point2D p2 = new Point2D(11000000.0, 3500000.0); Point2D p3 = new Point2D(12128888.89, 3500000.0); Point2D[] point2ds = { p1, p2, p3 }; // The measurement parameter MeasureParameter measureParameter = new MeasureParameter(); measureParameter.unit = Unit.METER; // Implement the measurement MeasureResult measureResult = mapc.measureArea("China", point2ds, measureParameter); // Dispose the resources mapc.dispose();
Detailed sample codes are shown: %SuperMap iServer_HOME%/samples/code/UseJavaAPI/MapComponentSample.