Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mstenta committed May 6, 2023
1 parent 3333b5f commit cd45acf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,29 @@ const xyzOpts = {
const xyzLayer = myMap.addLayer('xyz', xyzOpts);
```

### Reading Features

A `readFeatures` method is provided to enable reading geometry features from
various formats like GeoJSON and WKT. It takes a format as its first parameter,
and the input data as its second parameter.

For example:

```js
// Reading features from GeoJSON.
const geojson = {
type: 'Polygon',
coordinates: [
[[30, 10], [40, 40], [20, 40], [10, 20], [30, 10]]
]
}
const features = myMap.readFeatures('geojson', geojson);

// Reading features from Well-Known Text (WKT).
const wkt = "POLYGON ((-75.53643733263014 42.54424760416683, -75.5360350012779 42.54427527000766, -75.53589016199109 42.54412508386721, -75.53588747978209 42.54302634269183, -75.53643733263014 42.54424760416683))";
const features = myMap.readFeatures('wkt', wkt);
```

### Controlling the zoom level

There are two methods for controlling the zoom level. The first, `zoomToVectors`,
Expand Down
2 changes: 2 additions & 0 deletions src/instance/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import defaults from './defaults';

// Import instance methods.
import addLayer, { getLayerByName } from './methods/layer';
import readFeatures from './methods/features';
import addPopup from './methods/popup';
import { zoomToVectors, zoomToLayer } from './methods/zoom';
import { addBehavior, attachBehavior, attachBehaviorsByWeight } from './methods/behavior';
Expand Down Expand Up @@ -51,6 +52,7 @@ const createInstance = ({ target, options = {} }) => {
addLayer,
addPopup,
getLayerByName,
readFeatures,
zoomToVectors,
zoomToLayer,
addBehavior,
Expand Down
34 changes: 34 additions & 0 deletions src/instance/methods/features.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Import formats.
import GeoJSON from 'ol/format/GeoJSON';
import WKT from 'ol/format/WKT';

// Import the default projection configuration
import projection from '../../projection';

// Read features from Well Known Text (WKT).
function readFeaturesWKT(wkt) {
const isMultipart = wkt.includes('MULTIPOINT')
|| wkt.includes('MULTILINESTRING')
|| wkt.includes('MULTIPOLYGON')
|| wkt.includes('GEOMETRYCOLLECTION');
return isMultipart
? new WKT({ splitCollection: true }).readFeatures(wkt, projection)
: [new WKT().readFeature(wkt, projection)];
}

// Read features from GeoJSON.
function readFeaturesGeoJSON(geojson) {
return (new GeoJSON()).readFeatures(geojson, projection);
}

// Read features from various formats.
export default function readFeatures(type, input) {
let features;
if (type.toLowerCase() === 'geojson') {
features = readFeaturesGeoJSON(input);
}
if (type.toLowerCase() === 'wkt') {
features = readFeaturesWKT(input);
}
return features;
}
15 changes: 4 additions & 11 deletions src/instance/methods/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import LayerGroup from 'ol/layer/Group';
import VectorLayer from 'ol/layer/Vector';
import TileLayer from 'ol/layer/Tile';
import GeoJSON from 'ol/format/GeoJSON';
import WKT from 'ol/format/WKT';

// Import setWithCredentials function.
import { setWithCredentials } from 'ol/featureloader';
Expand All @@ -17,8 +16,8 @@ import { setWithCredentials } from 'ol/featureloader';
import * as Style from 'ol/style';
import colorStyles, { clusterStyle } from '../../styles';

// Import the default projection configuration
import projection from '../../projection';
// Import readFeatures function.
import readFeatures from './features'

Check failure on line 20 in src/instance/methods/layer.js

View workflow job for this annotation

GitHub Actions / Run build

Missing semicolon

// Set withCredentials to true for all XHR requests made via OpenLayers'
// feature loader. Typically farmOS requires authentication in order to
Expand Down Expand Up @@ -90,7 +89,7 @@ function addGeoJSONLayer({
});
} else {
source = new VectorSource({
features: (new GeoJSON()).readFeatures(geojson, projection),
features: readFeatures('geojson', geojson),
format,
attributions,
});
Expand Down Expand Up @@ -130,13 +129,7 @@ function addWKTLayer({
const style = styleFunction
? (feature, resolution) => styleFunction(feature, resolution, Style)
: colorStyles(color);
const isMultipart = wkt.includes('MULTIPOINT')
|| wkt.includes('MULTILINESTRING')
|| wkt.includes('MULTIPOLYGON')
|| wkt.includes('GEOMETRYCOLLECTION');
const features = isMultipart
? new WKT({ splitCollection: true }).readFeatures(wkt, projection)
: [new WKT().readFeature(wkt, projection)];
const features = readFeatures('wkt', wkt);
const attributions = [attribution];
const source = new VectorSource({
features,
Expand Down

0 comments on commit cd45acf

Please sign in to comment.