I have come code: Is it possible to get coordinates of allready drawn points ? Or get intersection with other feature when drawing? Yes. Possible all! Here is code, which gives you all intersecion with calculation of intersected segments (native openlayers works strange it counts just beetween start coord of LineString and end coord)
app.current.interaction = new ol.interaction.Draw({
features: app.current.featureOverlay.getFeatures(),
type: featureType.drawType
});
app.current.interaction.on('drawstart', function () {
app.current.interaction.lastEvent = 'drawstart';
});
$(window.map.getViewport()).on('mousemove',function(){
window.map.getLayers().forEach(function (layer) {
var features = layer.getSource().getFeatures();
if (features.length > 0) {
features.forEach(function (feature) {
if (app.current.drawFeatureData !== null) {
var geom = app.current.drawFeatureData.getGeometry(); //get current drawFeature data from "startdrag" Draw event"
if (geom) {
var drawCoords = geom.getCoordinates();
var featureCoords = feature.getGeometry().getCoordinates();
var index;
if (drawCoords.length > 2) {
index = drawCoords.length - 2;
} else {
index = 0;
}
var x1 = drawCoords[index][0];
var y1 = drawCoords[index][1];
var x2 = drawCoords[drawCoords.length - 1][0];
var y2 = drawCoords[drawCoords.length - 1][1];
feature.setStyle(app.styles.line);
app.current.hasBarrierIntersection = null; // global trigger if has intersection
//app.current.drawFeatureData.setStyle(...); // set style for draw feature
//get segments of LineString
if (featureCoords[0].length > 0) {
for (var i = 0; i < featureCoords[0].length; i++) {
var firstCoord = featureCoords[0][i];
var nextCoord = featureCoords[0][i + 1];
if (nextCoord) {
var x3 = firstCoord[0];
var y3 = firstCoord[1];
var x4 = nextCoord[0];
var y4 = nextCoord[1];
var result = app.getIntersection(x1, y1, x2, y2, x3, y3, x4, y4);
if (result.line1 && result.line2) {
app.current.drawFeatureData.setStyle(app.styles.routeLineIntersected);
feature.setStyle(app.styles.lineIntersected);
app.current.hasBarrierIntersection = true;
}
}
}
}
}
}
});
}
});
});
Here is code which counts intersection with 4 coords (i've found it on stackoverflow, can't find link):
app = {
//returns line1 = true - if line 1 intersects as infinte vector, line2 = true - if line2 intersects as infinite vector, line1 && line2 - if segments are intersected, x,y - coords of intersection
getIntersection: function (x1, y1, x2, y2, x3, y3, x4, y4) {
var denominator, a, b, numerator1, numerator2, result = {
x: null,
y: null,
line1: false,
line2: false
};
denominator = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1));
if (denominator == 0) {
return result;
}
a = y1 - y3;
b = x1 - x3;
numerator1 = ((x4 - x3) * a) - ((y4 - y3) * b);
numerator2 = ((x2 - x1) * a) - ((y2 - y1) * b);
a = numerator1 / denominator;
b = numerator2 / denominator;
result.x = x1 + (a * (x2 - x1));
result.y = y1 + (a * (y2 - y1));
if (a > 0 && a < 1) {
result.line1 = true;
}
if (b > 0 && b < 1) {
result.line2 = true;
}
return result;
}
}
Aucun commentaire:
Enregistrer un commentaire