Tiny WFS-T client to insert (drawing/uploading), modify and delete features on GeoServers using OpenLayers. Layers with these types of geometries are supported: GeometryCollection (in this case, you can choose the geometry type of each element to draw), Point, MultiPoint, LineString, MultiLineString, Polygon and MultiPolygon.
Tested with OpenLayers version 5, 6, 7, 8 and 9.
See Wfst Options for more details.
import Wfst, { Geoserver, WmsLayer, WfsLayer } from 'ol-wfst';
import { Fill, Stroke, Circle, Style } from 'ol/style';
// Style
import 'ol-wfst/lib/scss/ol-wfst.css';
import 'ol-wfst/lib/scss/ol-wfst.bootstrap5.css'; // Do not import if you already have boostrap css
// Optional credentials
const password = 123456;
const username = 'username';
const geoserver = {
url: 'https://mysite.com/geoserver/myworkspace/ows',
advanced: {
getCapabilitiesVersion: '2.0.0',
getFeatureVersion: '1.0.0',
describeFeatureTypeVersion: '1.1.0',
lockFeatureVersion: '1.1.0',
projection: 'EPSG:3857'
},
// Maybe you wanna add this on a proxy, at the backend
headers: { Authorization: 'Basic ' + btoa(username + ':' + password) }
};
const wfsLayer = new WfsLayer({
name: 'myPointsLayer', // Name of the layer on the GeoServer
label: 'Photos', // Optional Label to be displayed in the controller
geoserver: geoserver,
zIndex: 99,
// WfsLayers need a custom style
style: new Style({
image: new Circle({
radius: 7,
fill: new Fill({
color: '#000000'
}),
stroke: new Stroke({
color: [255, 0, 0],
width: 2
})
})
}),
// See [geoserverVendor](#geoservervendor) for more details.
geoserverVendor: {
cql_filter: 'id > 50',
maxFeatures: 500
},
beforeTransactFeature: function (feature, transactionType) {
if (transactionType === 'insert') {
// Maybe add a custom value o perform an action before insert features
feature.set('customProperty', 'customValue', true);
} else if (transactionType === 'update') {
// ... //
} else if (transactionType === 'delete') {
// .. //
}
return feature;
}
});
const wmsLayer = new WmsLayer({
name: 'myMultiGeometryLayer',
label: 'Other elements',
geoserver: geoserver,
// See [geoserverVendor](#geoservervendor) for more details.
geoserverVendor: {
cql_filter: 'id > 50',
buffer: 50,
maxFeatures: 500
},
beforeTransactFeature: function (feature, transactionType) {
if (transactionType === 'insert') {
// Maybe add a custom value o perform an action before insert features
feature.set('customProperty', 'customValue', true);
} else if (transactionType === 'update') {
// ... //
} else if (transactionType === 'delete') {
// .. //
}
return feature;
},
beforeShowFieldsModal: function (field, value, formElement) {
// Transform default input to a custom select
if (field.name === 'source') {
const createSelect = (name, options) => {
const createOption = (opt) => {
const option = document.createElement('option');
option.value = opt.value;
option.label = opt.label;
if (opt.value === value) {
option.selected = true;
}
return option;
};
const select = document.createElement('select');
select.className = 'ol-wfst--input-field-input';
select.name = name;
options.forEach((opt) => {
select.appendChild(createOption(opt));
});
return select;
};
return createSelect('source', [
{ label: 'Select source', value: '' },
{ label: 'Option 1', value: 'option1' },
{ label: 'Option 2', value: 'option2' }
]);
} else if (field.name === 'registroid') {
// add default class to the HTMLElement in a particular field
formElement.classList.add('custom-field-class');
}
return formElement;
}
});
const wfst = new Wfst({
layers: [wfsLayer, wmsLayer],
language: 'en',
showUpload: true
});
map.addControl(wfst);
See [#wfsSource]
wmsLayer.setCustomParam('cql_filter', `id = 28`);
wmsLayer.setCustomParam('env', `tags=true`);
const feature = new ol.Feature({
geometry: new ol.geom.MultiPoint([[`-57.1145}`, `-36.2855`]])
});
const inserted = await myWmsLayer.insertFeatures([feature]);
if (inserted) {
alert('Feature inserted');
} else {
alert('Feature not inserted');
}
wfst.on(
['modifystart', 'modifyend', 'drawstart', 'drawend', 'load', 'visible'],
function (evt) {
console.log(evt);
}
);
wfst.on(['describeFeatureType'], function (evt) {
console.log(evt.layer, evt.data);
});
geoserver.on(['getCapabilities'], function (evt) {
console.log(evt.data);
});
wmsLayer.on(['change:describeFeatureType'], function (evt) {
console.log(evt);
});
Source TileWMS Events
wmsLayer.getSource().on([...])
wfsLayer.on(['change:describeFeatureType'], function (evt) {
console.log(evt);
});
wfsLayer.getSource().on([...])
- If the features/vertex appear to be slightly offset after adding them, check the Number of Decimals in your Workplace, you may have to increment that to have a more accurete preview.
- You can configure a Basic Authentication or an HTTP Header Proxy Authentication with this client, but in some cases is recommended setting that on an reverse proxy on the backend.
- If you don't use a reverse proxy, remeber configure cors
See CHANGELOG for details of changes in each release.
Load ol-wfst.js
after OpenLayers. The available classes are Wfst
, Wfst.Geoserver
, Wfst.WfsLayer
and Wfst.WmsLayer
.
<script src="https://unpkg.com/ol-wfst@4.4.0"></script>
<link rel="stylesheet" href="https://unpkg.com/ol-wfst@4.4.0/dist/css/ol-wfst.min.css" />
<link rel="stylesheet" href="https://unpkg.com/ol-wfst@4.4.0/dist/css/ol-wfst.bootstrap5.min.css" />
NPM package: ol-wfst.
Install the package via npm
npm install ol-wfst
import Wfst, { Geoserver, WmsLayer, WfsLayer } from 'ol-wfst';
// scss
import 'ol-wfst/lib/style/scss/ol-wfst.scss';
import 'ol-wfst/lib/style/scss/-ol-wfst.bootstrap5.scss';
// or css
import 'ol-wfst/lib/style/css/ol-wfst.css';
import 'ol-wfst/lib/style/css/ol-wfst.bootstrap5.css';
TypeScript types are shipped with the project in the dist directory and should be automatically used in a TypeScript project. Interfaces are provided for Wfst Options.
- BaseGeoserverVendor
- Wfst
- Geoserver
- WfsLayer
- WmsLayer
- Options
- LayerOptions
- GeoserverOptions
- GeoServerAdvanced
- WfsGeoserverVendor
- WmsGeoserverVendor
- IProperty
- IGeoserverDescribeFeatureType
- I18n
Add any other param
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#cql-filter
Type: string
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#sortBy
Type: string
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#featureid
Type: string
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#filter
Type: string
WMS: https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#format-options WFS: https://docs.geoserver.org/latest/en/user/services/wfs/vendor.html#format-options
Type: string
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#maxfeatures-and-startindex
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#maxfeatures-and-startindex
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#propertyname
Type: string
Extends ol/control/Control~Control
Tiny WFS-T client to insert (drawing/uploading), modify and delete features on GeoServers using OpenLayers. Layers with these types of geometries are supported: "GeometryCollection" (in this case, you can choose the geometry type of each element to draw), "Point", "MultiPoint", "LineString", "MultiLineString", "Polygon" and "MultiPolygon".
options
Options? Wfst options, see Wfst Options for more details.
Get all the layers in the ol-wfst instance
Returns Array<(WfsLayer | WmsLayer)>
Get a layer
layerName
(optional, default''
)
Activate/deactivate the draw mode
Returns void
Activate/desactivate the edit mode
bool
(optional, defaulttrue
)
Returns void
Extends ol/Object~BaseObject
options
GeoserverOptions
Returns XMLDocument
Only work for 2.0.0
getCapabilities version
Returns any
url
stringopt_silent
(optional, defaultfalse
)
Returns void
Returns string
headers
HeadersInit (optional, default{}
)opt_silent
(optional, defaultfalse
)
Returns void
Returns HeadersInit
credentials
RequestCredentials (optional, defaultnull
)opt_silent
(optional, defaultfalse
)
Returns void
Returns RequestCredentials
advanced
GeoServerAdvanced (optional, default{}
)opt_silent
(optional, defaultfalse
)
Returns void
Returns GeoServerAdvanced
Returns boolean
Returns boolean
Returns boolean
useLockFeature
booleanopt_silent
(optional, defaultfalse
)
Returns void
Returns boolean
Get the capabilities from the GeoServer and check all the available operations.
Returns Promise<XMLDocument>
features
Array<Feature<Geometry>>
Lock a feature in the geoserver. Useful before editing a geometry, to avoid changes from multiples suers
Extends ol/layer/Vector~VectorLayer
Layer to retrieve WFS features from geoservers https://docs.geoserver.org/stable/en/user/services/wfs/reference.html
options
LayerOptions
Use this to update Geoserver Wms Vendors (https://docs.geoserver.org/latest/en/user/services/wms/vendor.html) and other arguements (https://docs.geoserver.org/stable/en/user/services/wms/reference.html#getmap) in all the getMap requests.
Example: you can use this to change the style of the WMS, add a custom sld, set a cql_filter, etc.
paramName
stringvalue
string Useundefined
ornull
to remove the param (optional, defaultnull
)refresh
(optional, defaulttrue
)
Returns URLSearchParams
Extends ol/layer/Tile~TileLayer
Layer to retrieve WMS information from geoservers https://docs.geoserver.org/stable/en/user/services/wms/reference.html
options
LayerOptions
Use this to update Geoserver Wfs Vendors (https://docs.geoserver.org/latest/en/user/services/wfs/vendor.html) and other arguements (https://docs.geoserver.org/stable/en/user/services/wfs/reference.html) in all the getFeature requests.
Example: you can use this to set a cql_filter, limit the numbers of features, etc.
Returns URLSearchParams
[interface] - Wfst Options specified when creating a Wfst instance
Default values:
{
layers: null,
evtType: 'singleclick',
active: true,
showControl: true,
language: 'en',
i18n: {...}, // according to language selection
uploadFormats: '.geojson,.json,.kml',
processUpload: null,
}
Triggered to allow implement custom functions or to parse other formats than default by filtering the extension. If this doesn't return features, the default function will be used to extract them.
file
File
Returns Array<Feature<Geometry>>
Layers to be loaded from the geoserver
Type: Array<(WfsLayer | WmsLayer)>
Init active
Type: boolean
The click event to allow selection of Features to be edited
Type: ("singleclick"
| "dblclick"
)
Show/hide the control map
Type: boolean
Modal configuration
Type: {animateClass: string?, animateInClass: string?, transition: number?, backdropTransition: number?, templates: {dialog: (string | HTMLElement)?, headerClose: (string | HTMLElement)?}?}
Language to be used
Type: ("es"
| "en"
| "zh"
)
Custom translations
Type: I18n
Show/hide the upload button
Type: boolean
Accepted extension formats on upload Example: ".json,.geojson"
Type: string
Extends Omit<VectorLayerOptions<any>, 'source'>
[interface] - Parameters to create the layers and connect to the GeoServer
You can use all the parameters supported by OpenLayers
Default values:
{
name: null,
geoserver: null,
label: null, // `name` if not provided
strategy: all,
geoserverVendor: null
}
Triggered before inserting new features to the Geoserver. Use this to insert custom properties, modify the feature, etc.
feature
Feature<Geometry>transaction
TransactionType
Returns Feature<Geometry>
Layer name in the GeoServer
Type: string
Geoserver Object
Type: Geoserver
Label to be displayed in the widget control
Type: string
Available geoserver options
Type: (WfsGeoserverVendor | WmsGeoserverVendor)
Strategy function for loading features.
Only for WFS
By default all
strategy is used
Type: LoadingStrategy
Hook to customize the html elements showed in the fields modal
Return null
to hide the field from the modal
Type: function (field: IProperty, value: string, formElement: HTMLElement): (HTMLElement | string | null)
[interface]
Url for OWS services. This endpoint will recive the WFS, WFST and WMS requests
Type: string
Advanced options for geoserver requests
Type: GeoServerAdvanced
Http headers for GeoServer requests https://developer.mozilla.org/en-US/docs/Web/API/Request/headers
Type: HeadersInit
Credentials for fetch requests https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
Default is 'same-origin'
Type: RequestCredentials
Use LockFeatue request on GeoServer when selecting features. Prevents a feature from being edited through a persistent feature lock. This is not always supportedd by the GeoServer. See https://docs.geoserver.org/stable/en/user/services/wfs/reference.html
Type: boolean
[interface]
- Default values:
{
getCapabilitiesVersion: '2.0.0',
getFeatureVersion: '1.0.0',
describeFeatureTypeVersion: '1.1.0',
lockFeatureVersion: '1.1.0',
wfsTransactionVersion: '1.1.0',
projection: 'EPSG:3857',
lockFeatureParams: {
expiry: 5,
lockId: 'Geoserver',
releaseAction: 'SOME'
}
}
Extends BaseGeoserverVendor
[interface] - WFS geoserver options https://docs.geoserver.org/latest/en/user/services/wfs/vendor.html
https://docs.geoserver.org/latest/en/user/services/wfs/vendor.html#xml-request-validation
Type: boolean
Extends BaseGeoserverVendor
[interface] - WMS geoserver options https://docs.geoserver.org/latest/en/user/services/wms/vendor.html
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#buffer
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#env
Type: string
https://docs.geoserver.org/latest/en/user/services/wms/vendor.html#clip
Type: string
Styles in which layers are to be rendered. Value is a comma-separated list of style names, or empty if default styling is required. Style names may be empty in the list, to use default layer styling.
Type: string
Whether the map background should be transparent. Values are true or false. Default is false
Type: boolean
Background color for the map image. Value is in the form RRGGBB. Default is FFFFFF (white).
Type: string
Time value or range for map data. See Time Support in GeoServer WMS for more information.
Type: string
A URL referencing a StyledLayerDescriptor XML file which controls or enhances map layers and styling
Type: string
A URL-encoded StyledLayerDescriptor XML document which controls or enhances map layers and styling
Type: string
[interface] - Geoserver original layer properties response on DescribeFeature request
[interface] - Geoserver original response on DescribeFeature request
DescribeFeature request parsed
Type: {namespace: string, properties: Array<IProperty>, geomType: GeometryType, geomField: string}
[interface] - Custom Language specified when creating a WFST instance
Labels section
Type: {select: string?, addElement: string?, editElement: string?, save: string?, delete: string?, cancel: string?, apply: string?, upload: string?, editMode: string?, confirmDelete: string?, geomTypeNotSupported: string?, editFields: string?, editGeom: string?, selectDrawType: string?, uploadToLayer: string?, uploadFeatures: string?, validFeatures: string?, invalidFeatures: string?, loading: string?, toggleVisibility: string?, close: string?}
Errors section
Type: {capabilities: string?, wfst: string?, layer: string?, layerNotFound: string?, layerNotVisible: string?, noValidGeometry: string?, geoserver: string?, badFormat: string?, badFile: string?, lockFeature: string?, transaction: string?, getFeatures: string?}
- ~~Add support to diferent layer styles~~
- ~~Improve widget controller: visibility toggle~~
- ~~Add events~~
- Add
Don't show again
option in the error modal - Allow selection of multiples features and bulk edit
- Add customizables styles
- Improve scss (add variables)
- Add cookies to persist widget controller state
- Geometry type LinearRing support
- Tests!
- Improve comments and documentation
- Improve interface
- Change svg imports to preserve svg structure
- Improve style on editing and drawing features