OpenLayers 3: map Vector layer (ol. layer. Vector), openlayersvector
In GIS, maps are generally divided into two categories: raster maps and vector maps. raster maps are actually digital photos, but some are satellite photos. They share a common feature, that is, they are all composed of multiple pixels. The pixel size is consistent, and the Row Height and column width are consistent. From this perspective, A remote sensing image is like a grid.
A vector map is composed of many elements. Each element has its own geographic coordinates. based on mathematical rules, no matter how the vector map is enlarged, the map will not be distorted. It isOpenLayers
Is a very important layer type, using vector map can achieve a lot of functions, such as dynamic plotting, callingWFS
Service, editing elements, clickable elements, dynamic loading elements, and so on.
Composition of vector layers
The vector layer is rendered on the client and corresponds to the browser in the web environment. A vector layer consists of a data source and a style. The data forms the elements of the vector layer, and the style specifies the display mode and appearance of the elements. An initialized vector layer contains one or more features, each of which consists of a geographical attribute and multiple other attributes, which may contain names. The structure is as follows:
When initializing a vector layer, you can configure the behavior and appearance of the vector layer with the following options:
- Brightness, contrast, layer brightness and contrast are both numerical values;
- RenderOrder, a function that specifies a rule (function (ol. Feature, ol. Feature ));
- Hue, tone, is a numerical value;
- MinResolution: Minimum visible resolution;
- MaxResolution, the maximum visible resolution;
- Opacity, transparency of layers, 0 ~ Value Between 1, 1 indicates opacity;
- Saturation, saturation;
- Source, layer data source;
- Style, layer style, An ol. Style. style or an ol. Style. style array, or a function that returns ol. Style. style;
- Visible, layer visibility, default value:
true
.
Initialize a vector Layer
Use an example to illustrate how to initialize a vector layer:
var vector = new ol.layer.Vector({ source: new ol.source.Vector({ url: 'data/china_province_boundries.geojson', projection: 'EPSG:3857', format: new ol.format.GeoJSON({ extractStyles: false }) }), style: style});
In this examplesource
The url sets the data source, the projection sets the geographic coordinate system, and the format sets the data parser. Because the data source specified by the url is in geojson format, the parser is also a geojson parser.new ol.format.GeoJSON
.
Acquisition Element
So how to obtain a certain feature of a vector Layer? The general idea is vector. getFeature () is actually not. The data of the vector is contained in the source. To obtain the feature data of the vector, it must be in the source, for example, vector. getSource (). getFeatures (), this function returns a feature array, directly using [], you can use it, or get it according to the element ID (getFeatureById ()).
Similarly, as long as data-related operations are performed, the source instance of the vector needs to be obtained and then operated, for example, adding elements (addFeature) and deleting elements (removeFeature ), perform the same operation (forEachFeature) on each element ).
Geometry
GetGeometry () can be used to obtain the geographical attributes of elements. Of course, this function returns the geometry contained by the elements. geometry contains many types, mainly include point, multi point, linear ring, line string, multi line string, polygon, multi polygon, and circle.
After geometry is obtained, you can obtain the coordinates of the elements. You can make some geographic judgments based on the coordinates, such as determining whether a coordinate is within the element (containsCoordinate(coordinate)
OrcontainsXY(x, y)
) To obtain the central coordinates of the elements.
Summary
Using vector layers, you can achieve many functions, such as dynamic loading of vector data and callingWFS
Service, dynamic plotting, and editing elements are performed at the layer level and element level respectively.
For example, different images can be drawn on the vector layer, and attributes can be added, and then updated to the database, that is, the dynamic plotting system; or element-level data can be dynamically loaded, such as tracking the vehicle trajectory.