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.

Geometry disappear and no show in openlayer map

See original GitHub issue

Describe the bug I have a really weird behavior when I add polygon on the map. I cope the code from https://openlayers.org/en/latest/examples/geojson.html

What I did is basically add three different sizes polygons to the map. However three polygons have three behavior .

  1. one appears normally (I put a //beijing comment right before the data for this polygon)
  2. one disappear after zoom in (I put a //ocean comment right before the data for this polygon)
  3. one never shows up (I put a //seattlecomment right before the data for this polygon)

To Reproduce copy the code below and put that in any openlayer app

import "ol/ol.css";
import Circle from "ol/geom/Circle";
import Feature from "ol/Feature";
import GeoJSON from "ol/format/GeoJSON";
import Map from "ol/Map";
import View from "ol/View";
import { Circle as CircleStyle, Fill, Stroke, Style } from "ol/style";
import { OSM, Vector as VectorSource } from "ol/source";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";
import { Polygon } from "ol/geom";

var geojsonObject = {
  type: "FeatureCollection",
  crs: {
    type: "name",
    properties: {
      name: "EPSG:3857",
    },
  },
  features: [
    {
      type: "Feature",
      geometry: {
        type: "Point",
        coordinates: [0, 0],
      },
    },
    {
      type: "Feature",
      geometry: {
        type: "LineString",
        coordinates: [
          [4e6, -2e6],
          [8e6, 2e6],
        ],
      },
    },
    {
      type: "Feature",
      geometry: {
        type: "LineString",
        coordinates: [
          [4e6, 2e6],
          [8e6, -2e6],
        ],
      },
    },
    {
      type: "Feature",
      geometry: {
        type: "Polygon",
        coordinates: [
          [
            [-5e6, -1e6],
            [-4e6, 1e6],
            [-3e6, -1e6],
          ],
        ],
      },
    },
    {
      type: "Feature",
      geometry: {
        type: "MultiLineString",
        coordinates: [
          [
            [-1e6, -7.5e5],
            [-1e6, 7.5e5],
          ],
          [
            [1e6, -7.5e5],
            [1e6, 7.5e5],
          ],
          [
            [-7.5e5, -1e6],
            [7.5e5, -1e6],
          ],
          [
            [-7.5e5, 1e6],
            [7.5e5, 1e6],
          ],
        ],
      },
    },
    {
      type: "Feature",
      geometry: {
        type: "MultiPolygon",
        coordinates: [
          [
            [
              [-5e6, 6e6],
              [-5e6, 8e6],
              [-3e6, 8e6],
              [-3e6, 6e6],
            ],
          ],
          [
            // ocean
            [
              [24450065.111636, 5579291.568592],
              [25448026.952927, 5364044.896941],
              [24816962.847405, 4507950.180147],
            ],
          ],
          [
            //beijing
            [
              [12956472.811422, 4853889.241058],
              [12956636.434435, 4853874.909115],
              [12956580.300993, 4853831.913287],
            ],
          ],
          [
            //seattle
            [
              [26455839.616239, 6042958.582169],
              [26457148.600348, 6042949.027541],
              [26456919.289263, 6041439.396232],
            ],
          ],
          [
            [
              [1e6, 6e6],
              [1e6, 8e6],
              [3e6, 8e6],
              [3e6, 6e6],
            ],
          ],
        ],
      },
    },
    {
      type: "Feature",
      geometry: {
        type: "GeometryCollection",
        geometries: [
          {
            type: "LineString",
            coordinates: [
              [-5e6, -5e6],
              [0, -5e6],
            ],
          },
          {
            type: "Point",
            coordinates: [4e6, -5e6],
          },
          {
            type: "Polygon",
            coordinates: [
              [
                [1e6, -6e6],
                [2e6, -4e6],
                [3e6, -6e6],
              ],
            ],
          },
        ],
      },
    },
  ],
};

const vectorSource = new VectorSource({
  features: new GeoJSON().readFeatures(geojsonObject),
});

var GEOLayer = new VectorLayer({
  source: vectorSource,
  //style: styleFunction,
});

const map = new Map({
  target: "map",
  layers: [
    new TileLayer({
      source: new OSM(),
    }),
  ],
  view: new View({
    center: [0, 0],
    zoom: 2,
  }),
});

map.addLayer(GEOLayer);

Expected behavior Three polygon should all appear and will not disappear when I zoom in/out

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
MoonEcommented, Jul 12, 2021

This should do:

const wrapEpsg3857 = (c) => {
  c[0] = c[0] % 40075016.68557849;
  if (c[0] < -20037508.342789244) {
    c[0] += 40075016.68557849;
  } else if (c[0] > 20037508.342789244) {
    c[0] -= 40075016.68557849;
  }
  return c;
}

console.log(
  [
    [24450065.111636, 5579291.568592],
    [25448026.952927, 5364044.896941],
    [24816962.847405, 4507950.180147],
    [24450065.111636, 5579291.568592],
  ].map(wrapEpsg3857)
);
0reactions
jw1996commented, Jul 12, 2021
const wrapEpsg3857 = (c) => {
  c[0] = c[0] % 40075016.68557849;
  if (c[0] < -20037508.342789244) {
    c[0] += 40075016.68557849
  }
  return c;
}

So helpful!!! Really appreciate your help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Openlayers 3 - how to keep features from disappear when ...
ol.layer.Vector has a property called renderBuffer . This property will prevent the icons from disappearing if you set this to the pixel value...
Read more >
Disappearing shapes after selection · Issue #10008 - GitHub
Sometimes shapes disappear after selection. Let's say that we have piece of map with ~1000 objects. We select one of them then move...
Read more >
Polygons disappear after drawing in TMS Layered openlayers ...
As soon as I draw the polygon or place the point on the map, the polygon/point disappears. Am I missing anything? How do...
Read more >
OpenLayers v7.2.2 API - Class: Layer
A visual representation of raster or vector map data. Layers group together those properties that pertain to how the data is to be...
Read more >
Integrating an OpenLayers map in Vue.js, a step-by-step guide
On the other hand, you may or may not have heard about OpenLayers, ... see it appear on the map; edit an object...
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