Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to OL3Parser to create LineStrings from LinearRings #307

Open
dbaston opened this issue Apr 26, 2017 · 1 comment
Open

Add option to OL3Parser to create LineStrings from LinearRings #307

dbaston opened this issue Apr 26, 2017 · 1 comment
Milestone

Comments

@dbaston
Copy link

dbaston commented Apr 26, 2017

The LinearRing is not a "complete" type in OpenLayers, and cannot be rendered on a map. (See http://openlayers.org/en/master/apidoc/ol.geom.LinearRing.html)

It would be useful to pass an argument to the OL3Parser instructing it to generate an OpenLayers LineString from LinearRing inputs.

@bjornharrtell bjornharrtell self-assigned this Jul 23, 2017
@bjornharrtell bjornharrtell added this to the 1.5.0 milestone Jul 23, 2017
@bjornharrtell bjornharrtell modified the milestones: 1.5.0, Future Nov 26, 2017
@bjornharrtell bjornharrtell removed their assignment Nov 26, 2019
@dehmer
Copy link

dehmer commented Sep 3, 2022

I have created the following workaround some time ago and had no troubles so far.

import GeometryFactory from 'jsts/org/locationtech/jts/geom/GeometryFactory.js'
import OL3Parser from 'jsts/org/locationtech/jts/io/OL3Parser.js'
import GeoJSONReader from 'jsts/org/locationtech/jts/io/GeoJSONReader.js'
import * as geom from 'ol/geom.js'

export { GeoJSONReader }

const K = v => fn => { fn(v); return v }
const geometryFactory = new GeometryFactory()

/**
 * Setup JST/OL parser to convert between JST and OL geometries.
 * REFERENCE: http://bjornharrtell.github.io/jsts/1.6.1/doc/module-org_locationtech_jts_io_OL3Parser.html
 */
const parser = K(new OL3Parser(geometryFactory))(parser => parser.inject(
  geom.Point,
  geom.LineString,
  geom.LinearRing,
  geom.Polygon,
  geom.MultiPoint,
  geom.MultiLineString,
  geom.MultiPolygon,
  geom.GeometryCollection
))

/**
 * JSTS ignores the fact that ol.geom.LinearRing is not supposed to be rendered.
 * We convert LinearRing to equivalent LineString.
 */
/* eslint-disable no-proto */
const convertToLinearRing = parser.__proto__.convertToLinearRing
parser.__proto__.convertToLinearRing = function (linearRing) {
  const geometry = convertToLinearRing.call(this, linearRing)
  return new geom.LineString(geometry.getCoordinates())
}

export const read = olGeometry => parser.read(olGeometry)
export const write = jtsGeometry => parser.write(jtsGeometry)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants