question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a method to rotate the squareGrid?

See original GitHub issue

Is there a way to rotate the squareGrid?

Here’s what i have right now.

Link to source

What i want is for the grid to rotate to align with the angle of a polygon shape in google maps.

gridcells

var map = new google.maps.Map(document.getElementById("map"), {
  zoom: 3,
  maxZoom: 8,
  minZoom: 2,
  center: {
    lat: 0,
    lng: -180
  },
  streetViewControl: false,
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var triangleCoords = [
  { lat: 25.774, lng: -80.19 },
  { lat: 18.466, lng: -66.118 },
  { lat: 32.321, lng: -64.757 },
  { lat: 25.774, lng: -80.19 }
];

// Construct the polygon.
var bermudaTriangle = new google.maps.Polygon({
  paths: triangleCoords,
  strokeColor: "#FF0000",
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: "#FF0000",
  fillOpacity: 0.35
});
bermudaTriangle.setMap(map);

const polygonMap = bermudaTriangle.getMap();
const projection = polygonMap.getProjection();

google.maps.Polygon.prototype.my_getBounds=function(){
    var bounds = new google.maps.LatLngBounds()
    this.getPath().forEach(function(element,index){bounds.extend(element)})
    return bounds
}

let coordinates = [];

// convert to bermudaTriangle path to geojson
for (let point of bermudaTriangle.getPath().getArray()) {
  coordinates.push([point.lng(), point.lat()]);
}

const bounds = bermudaTriangle.my_getBounds();
const center = bounds.getCenter().toJSON();

const geojson = turf.polygon([coordinates]);

const pivot = [center.lng, center.lat];
const options = {pivot: pivot};
const rotatedPoly = turf.transformRotate(geojson, 0, options);
const bbox = turf.bbox(rotatedPoly);

const grid = turf.squareGrid(bbox, 50, {
  units: "miles",
  // mask: geojson
});

map.data.addGeoJson(grid);

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

3reactions
dpmcmlxxvicommented, May 9, 2019

@chan-dev The grid is a just feature collection. Just iterate over the collection with, for example, @turf/meta.featureEach and apply @turf/transform-rotate to each feature (i.e., cell).

2reactions
stebogitcommented, May 10, 2019

@chan-dev I don’t think there is really a more efficient way. I can only suggest a shorter/prettier, though not tested, version (no optimization, just a more compact form thanks to ES6 syntax):

const rotatedGrid = turf.featureCollection(
  grid.features.map((feature) => turf.transformRotate(feature, 10, {pivot}))
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to rotate shapes on a grid - 3 different methods! - YouTube
In this lesson I explain how to rotate shapes on squared paper, either 90 degrees or 180 degrees. I demonstrate 3 different methods...
Read more >
Rotating position in a square grid - Mathematics Stack Exchange
With this description, the problem turns out to be a purely geometrical one. You can rotate grids of arbitrary sizes (i.e. with arbitrary...
Read more >
Rotation (article) | Transformations - Khan Academy
Rotating the correct way​​ The correct way to rotate the square is to: Translate the coordinate system's origin (0, 0) to where you...
Read more >
Inplace rotate square matrix by 90 degrees | Set 1
Given a square matrix, turn it by 90 degrees in an anti-clockwise ... Note: An approach that requires extra space is already discussed...
Read more >
Rotating Regulars and Greedy Grids - Delta Thoughts
Anyways, this tilted square reveals an important property of grids: rotating a lattice point by 90° around another point gives you a new, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found