R
R
Roman Andreevich2018-12-05 06:56:38
JavaScript
Roman Andreevich, 2018-12-05 06:56:38

How to divide a polygon on the map into equal parts or continuation: how to collect an array from the coordinates of the selected area with a given step?

Colleagues, good day.
So the task is to divide the polygon into equal sectors!
In continuation of the topic How to collect an array from the coordinates of the selected area ... there was one more question. Many thanks to comrade @serginhold for help and my understanding))))))))))
So! Building a map on a leaflet. We select an area on it, draw the simplest rectangle - a polygon, we have its corners in coordinates:

this.rectangle = L.rectangle(bounds, this.options).addTo(this.map); // строим прямоугольник - полигон
let coordsConners = this.rectangle.getLatLngs()[0]; // координаты углов

Next, we get the lengths of the horizontal and vertical lines:
getLines(southWest, southEast, northEast) { // получаем длины вертикальной и горизонтальной линии

    this.points = [
    new L.LatLng(southWest[0], southWest[1]),
    new L.LatLng(southEast[0], southEast[1]),
    new L.LatLng(northEast[0], northEast[1])			

    ];

  this.rectangleData.horizont = +(this.points[0].distanceTo(this.points[1])).toFixed(0);
  this.rectangleData.vertical = +(this.points[1].distanceTo(this.points[2])).toFixed(0);

}

In fact, everything is simple here, we take southWest, southEast, northEast from coordsConners. this.rectangleData.horizont and this.rectangleData.vertical are the actual lengths of the lines in meters. In total, we have the coordinates of the corners and the lengths of the lines.
As suggested yesterday, comrade @serginhold (thanks again), and then, when I stopped being stupid, I did:
let x_min = coordsConners[1].lat;
    let x_max = coordsConners[3].lat;
    let y_min = coordsConners[1].lng;
    let y_max = coordsConners[3].lng;

  let x = 0;
  let y = 0;

    let count = 100;

    for (let i = 0; i < count; i++) {

      x = Math.random() * (x_max - x_min) + x_min;
      y = Math.random() * (y_max - y_min) + y_min;

      new L.marker([x, y]).addTo(this.map);

    }

Just randomly put markers on the map. This is accordingly not true. Because they are placed randomly. It is necessary to split this rectangle into equal parts. And here I ask for help, can anyone come across or just tell me. There are lengths and coordinates, how to split a rectangle?
ps Well, do not break it more precisely, but simply draw new, small ones. so that their height and width are no more than the number of meters set from config.
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AngReload, 2018-12-05
@AngReload

Is it necessary?

const RECOMMEND_SIZE = 10;
const BIAS_TO_CENTER = 0.5;

let x_min = coordsConners[1].lat;
let x_max = coordsConners[3].lat;
let y_min = coordsConners[1].lng;
let y_max = coordsConners[3].lng;

let x_side_len = x_max - x_min;
let y_side_len = y_max - y_min;

let x_total_count = Math.round(x_len / RECOMMEND_SIZE);
let y_total_count = Math.round(y_len / RECOMMEND_SIZE);

let x_closest_size = x_side_len / x_total_count;
let y_closest_size = y_side_len / y_total_count;


for (let x = 0; x < x_total_count; x++) {
  for (let y = 0; y < y_total_count; y++) {
    let coord_x = x_min + x_closest_size * (x + BIAS_TO_CENTER);
    let coord_y = y_min + y_closest_size * (y + BIAS_TO_CENTER);
    new L.marker([coord_x, coord_y]).addTo(this.map);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question