Coder Social home page Coder Social logo

Comments (14)

probins avatar probins commented on August 15, 2024

why would you want to split a point?

from ol2.

drnextgis avatar drnextgis commented on August 15, 2024

I would like to have tool for split another geometry by point not point itself.

from ol2.

ahocevar avatar ahocevar commented on August 15, 2024

Then Geometry.Point will not need a split method, but the split method in Geometry.LineString needs to be modified to work with a Point geometry as target.

from ol2.

drnextgis avatar drnextgis commented on August 15, 2024

I'm not quite clear about split method of Geometry.LineString. This method uses linestring for split a target geometry. But I've told about reverse operation - split linestring by point geometry. As I can see for this purpose method splitWith is used: return geometry.split(this, options); So I think for splitting linestring by point - point itself needs has split method.

from ol2.

probins avatar probins commented on August 15, 2024

What is it you're trying to achieve? With the split control, you draw a line on the map, and where that line crosses one or more linestrings, those linestrings are split at the point of intersection. The software works out where that intersection is in terms of coordinates. If you're entering a point, how would you know whether it is even on the linestring (with the exception of the vertices)? The segments of linestrings are simply straight lines drawn between the vertices at whatever scale/projected plane you're using. Are you wanting to split a linestring at one of its vertices, or are you thinking that the line would be changed to go to the point you enter?

from ol2.

drnextgis avatar drnextgis commented on August 15, 2024

Sorry if I confused you. I would like to split linestring by drawing point. Now native method for split line by point lacks and I need to emulate point by line. See my example: http://gis-lab.info/share/DR/sandbox/split.html Snapping can be used for putting point exact within linestring.

from ol2.

probins avatar probins commented on August 15, 2024

ok, I see what you mean. But the existing control works, doesn't it? I just went to your url above, where there is one linestring in map.layers[2].features. I moved your little rectangle to around halfway along the line, and clicked. There were then two linestring features in map.layers[2], split where I had clicked. Is that not what you want?

from ol2.

drnextgis avatar drnextgis commented on August 15, 2024

Yes, but in fact in this example I split linestring by another linestring. When I put point with (x,y) coordinates - invisible linestring is created in background with the following coordinates: strart point: (x-epsilon, y-epsilon), end point: (x+epsilon, y+epsilon), where epsilon is small constant (0.0001 in my example). It looks like hack, do you agree? In some case it may happen that the invisible line will be parallel to the line of splitted layer and my approach will not work.

from ol2.

probins avatar probins commented on August 15, 2024

ah, ok, I see what you're doing. Yes, that is quite devious :-)

And, yes, I think you're right. If you want to do this and use the existing split control and logic, you would need Point.split() which linestring.splitWith() would call, and which would take a linestring geometry and split it at its own coordinates. Alternatively, as Point.split() sounds illogical to me, change splitWith() so that if geometry is a Point, it would call linestring.splitWithPoint() or such like.

I'll leave others to decide whether that would be a useful addition to the library.

from ol2.

juanluisrp avatar juanluisrp commented on August 15, 2024

Of course,it would be very useful.

from ol2.

oburakevych avatar oburakevych commented on August 15, 2024

+1

from ol2.

aleksandrmelnyk avatar aleksandrmelnyk commented on August 15, 2024

it would be useful in situations when line needs to split by point programmatically, not from control etc.

from ol2.

aleksandrmelnyk avatar aleksandrmelnyk commented on August 15, 2024

Something like:

OpenLayers.Geometry.LineString.prototype.splitByVertex = function(vertex) {
var verts = this.getVertices(), i, l, p, points = {seg1:[], seg2: []},
lines = [], over = false;

for (i = 0, l = verts.length; i < l; i++) {
    if (!over && verts[i].toString() == vertex.toString()) {
        points.seg1.push(verts[i]);
        over = true;
    } 

    if (over) {
        points.seg2.push(verts[i]);
    } else {
        points.seg1.push(verts[i]);
    }

}

    for (p in points) {

            lines.push(new OpenLayers.Geometry.LineString(points[p]));
    }

    return lines

}

from ol2.

uhef avatar uhef commented on August 15, 2024

Below is an implementation that splits a given OpenLayers.Geometry.LineString by given OpenLayers.Geometry.Point. Line string is split from the point on the line string that is closest to the given point. One can enhance it to ignore points that lie too far from the line string.

Algorithm uses higher-order collection functions from Lo-Dash (like reduce and sortBy) but naturally the concept can be applied in contexts where such library is not available.

Function returns two arrays of OpenLayers.Geometry.Point - objects which define the two halfs of the split. One can use these to construct two OpenLayers.Geometry.LineString - objects.

  var splitLineStringByPoint = function(lineString, point) {
    var segments = _.reduce(lineString.getVertices(), function(acc, vertex, index, vertices) {
      if (index > 0) {
        var previousVertex = vertices[index - 1];
        var segmentGeometry = new OpenLayers.Geometry.LineString([previousVertex, vertex]);
        var distanceObject = segmentGeometry.distanceTo(point, { details: true });
        var segment = {
          distance: distanceObject.distance,
          splitPoint: {
            x: distanceObject.x0,
            y: distanceObject.y0
          },
          index: index - 1
        };
        return acc.concat([segment]);
      } else {
        return acc;
      }
    }, []);
    var splitSegment = _.head(_.sortBy(segments, 'distance'));
    var split = _.reduce(lineString.getVertices(), function(acc, vertex, index) {
      if (acc.firstSplit) {
        acc.firstSplitVertices.push(vertex);
        if (index === splitSegment.index) {
          acc.firstSplitVertices.push(new OpenLayers.Geometry.Point(splitSegment.splitPoint.x, splitSegment.splitPoint.y));
          acc.secondSplitVertices.push(new OpenLayers.Geometry.Point(splitSegment.splitPoint.x, splitSegment.splitPoint.y));
          acc.firstSplit = false;
        }
      } else {
        acc.secondSplitVertices.push(vertex);
      }
      return acc;
    }, {
      firstSplit: true,
      firstSplitVertices: [],
      secondSplitVertices: []
    });
    return [split.firstSplitVertices, split.secondSplitVertices];
  };

Also available as a gist: https://gist.github.com/uhef/d3d29052e41fba914579

from ol2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.