Map Elements

Table of contents

In JMap, map elements are objects (points, lines, polygons, text, etc.) that make up the map. The classes of these elements are all derived from the abstract class K2DElement. Map elements are organized into layers that are displayed in a JMap application within a view (View class). Each map element is associated with a geometry (Geometry interface) and has a digital identifier as well as a certain number of attributes. The map element does not directly contain any geometry information. Instead, it is the associated geometry that contains all of the geometry coordinates. Element identifiers found within the same layer must be unique. The GeneratedUniqueId() utility method of the K2DElement abstract class allows you to generate a sequence of unique identifiers for the elements.

Creating map elements

The following example demonstrates how to create map elements:

// Create a point geometry Point

pointGeometry = new Point.Double(100., 100.);

// Create the element using the geometry, null attributes and auto generated id

K2DPoint point = new K2DPoint(pointGeometry, null, K2DElement.generateUniqueId());

// Create a line geometry Point

pointGeometry1 = new Point.Double(100., 100.);

Point pointGeometry2 = new Point.Double(200., 200.);

Line lineGeometry = new Line(pointGeometry1, pointGeometry2);

// Create some attributes

String attrib1 = "some value";

Integer attrib2 = new Integer(999);

// Create the element using the geometry, 2 attributes and auto generated id

K2DPolyline line = new K2DPolyline(lineGeometry, new Object[] {attrib1, attrib2}, K2DElement.generateUniqueId());

Element attributes

Map elements normally have values for their attributes. These values are the descriptive data of the map elements. All elements within the same layer possess the same list of attributes (same names, same types). Note that the elements contain only the values of the attributes (table or chart of objects) and not their definition. The definition of these attributes is managed at the layer level, using the Attribute class.

The list of attributes for the elements of a layer is determined by the JMap administrator when the layer (linked attributes) is created. The types of attributes are defined using Java constants for the SQL types (java.sql.Types class).

Element attributes are used for many functions such as tooltips, labels, thematics, and filtering.

The following example demonstrates how to access the values of element attributes:

K2DElement element = ... Object[] attributeValues = element.getAttributes();

for (int i = 0; i < attributeValues.lenght; i++)

{

System.out.println(i + " : " + attributeValues[i]);

}

The following example demonstrates how to access the definition of a layer’s attributes.

VectorLayer layer= ...

Attribute[] attributes = layer.getAttributeMetaData();

for (int i = 0; i < attributes .lenght; i++)

{

System.out.println(i + " name : " + attributes[i].getName());

System.out.println(i + " title : " + attributes[i].getTitle());

System.out.println(i + " type: " + attributes[i].getType());

}

Element style

When elements are drawn on the map, a style object is used to determine the visual aspects of the elements. Instances of the Style class have properties such as line color, fill color, letter font, transparency, etc. Each layer will have one or more styles. The style used for displaying the elements is based on the scale of the map and whether or not it contains thematics. Thematics dictate the style of the displayed elements based on the attribute values of these elements.

The following example demonstrates how to obtain the style used for a layer according to a particular scale. Here the presence of thematics on the layer is not taken into account.

K2DElement element = ...

VectorLayer layer = ...

Style style = layer.getStyle(view.getScaleFactor());

The following example demonstrates how to obtain the style used to display a specific element at a given scale. The resulting style does not take into account the presence of thematics on the layer.

K2DElement element = ...

VectorLayer layer = ...

Style style = layer.getStyle(element, view.getScaleFactor());

Style properties can be modified by calling the methods of the Style class. The following example demonstrates how to modify the style parameters of a layer of polygons at the current map scale.

VectorLayer layerOfPolygons = ...

Style style = layerOfPolygons.getStyle(view.getScaleFactor());

style.setBorderColor(Color.BLACK);

style.setFillColor(Color.BLUE);

style.setTransparency(.5f); // 50% transparent

Bounding rectangle on display

The bounding rectangle of a displayed map element is the smallest rectangle in device coordinates (DC) that completely contains the element, taking its style into account. The style of an element influences the bounding rectangle displayed. For example, a thick polygon border will increase the size of the bounding rectangle, and the size of a point symbol will determine the size of the bounding rectangle on display, and so forth.

The following code example shows how to obtain the bounding rectangle of a displayed map element:

K2DElement element = ...

View view = ...

VectorLayer layer = ...

Rectangle displayBounds = element.getDisplayBounds(view.getTransform(), layer.getStyle(element, view.getScaleFactor()));

Note that the getDisplayBounds method takes the transformation of the view (refer to Coordinate Systems) and element style as parameters.

Selection

The map elements of vector layers can be selected. A variety of tools allow the user to select elements in the vector layers of a JMap project.

The K2DElement class has a property called selected that indicates whether or not an element is selected. Selected elements are displayed using a different style, which is the selection style of the vector layer.

Vector layers manage the list of their selected elements. The API of the VectorLayer class offers several methods related to selecting elements. See Layers and Layer Manager for more information on this topic.

The following source code example shows how to select and unselect elements.

K2DElement element = ...

VectorLayer layer = ...

// add the element to the current selection

layer.addToSelection(element);

// test if element is selected, useless, just to demonstrate boolean

isSelected = element.isSelected();

// cycle through selection

Collection selection = layer.getSelection();

for (K2DElement element : selection)

System.out.println(element));

// unselect element

layer.unselectElement(element);

// clear layer selection

layer.clearSelection();

Styled elements

Styled elements (K2DStyledElement class) are special map elements. They have their own style and they ignore the style of the layer that contains them when they are displayed. They are useful when programmatically adding elements to the map that have their own style. Aside from this difference, styled elements behave exactly like any other map elements.

The following coding example shows how to create styled elements.

K2DElement element = ...

Style style = ...

K2DStyledElement styledElem = new K2DStyledElement(element, style);

// element will use this style to draw itself

Dernière mise à jour

K2 Geospatial 2022