Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wkt.fromObject() fails for POLYGON or MULTIPOLYGON #135

Open
chmelej opened this issue Feb 13, 2019 · 6 comments
Open

wkt.fromObject() fails for POLYGON or MULTIPOLYGON #135

chmelej opened this issue Feb 13, 2019 · 6 comments

Comments

@chmelej
Copy link

chmelej commented Feb 13, 2019

Hi I have an example:

  var polygon = "POLYGON((30 10,10 20,20 40,40 40,30 10))";
    var mapOptions = {
        center: [50, 5],
        zoom: 16
    };

    var map = L.map('map', mapOptions);
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);

    var wkt = new Wkt.Wkt();
    wkt.read(polygon);
    var polygonOnMap = wkt.toObject(map.defaults);
    map.addLayer(polygonOnMap);
    map.fitBounds(polygonOnMap.getBounds());

    var serialize = wkt.write()
    
    if (serialize != polygon) {
       alert("ALERT1:" + serialize +" != "+polygon);
    } 

    var wkt2 = new Wkt.Wkt();
    wkt2.fromObject(polygonOnMap);
    var serialize2 = wkt2.write()

    if (serialize2 != polygon) {
       alert("ALERT2:" + serialize2 +" != "+polygon);
    } 

the polygon appears on the map but alert is triggered:

ALERT2:POLYGON((undefined undefined,undefined undefined)) != POLYGON((30 10,10 20,20 40,40 40,30 `10))

which is wrong :-(

I use a latest version of wicket.js, wicket-leaflet.js and leaflet.js 1.4.0

What am I doing wrong?
Thanks.

@chmelej chmelej changed the title wkt.fromObject() fails for POLYGON or MULTIPOLIGON wkt.fromObject() fails for POLYGON or MULTIPOLYGON Feb 13, 2019
@scottlepp
Copy link
Contributor

scottlepp commented Mar 18, 2019

@chmelej seems wicket-leaflet uses the old format of getLatLngs()

As a workaround, you could do something like:

  // WKT fromObject for leaflet follows leaflet 0.7 format
  const compatible = {
    _latlngs: polygonOnMap.getLatLngs()[0],
    getLatLngs: () => {
      return polygonOnMap.getLatLngs()[0];
    }
  }
  compatible.constructor = polygonOnMap.constructor;

  wkt.fromObject(compatible);

@arthur-e
Copy link
Owner

@scottlepp Thanks for figuring this out! Could you fix this in a Pull Request?

scottlepp added a commit to scottlepp/Wicket that referenced this issue Mar 18, 2019
arthur-e added a commit that referenced this issue Mar 19, 2019
#135 leaflet 1.x  getLatLng() now returns an array of arrays of coord…
@chmelej
Copy link
Author

chmelej commented Mar 21, 2019

Hi,
it's better. polygon is ok but multipolygon still not work:

var polygon = 'MULTIPOLYGON(((30 10,10 20,20 40,40 40,30 10),(60 40,40 50,50 70,70 70,60 40)))';

... returns

ALERT2:POLYGON((undefined undefined,undefined undefined,undefined undefined)) != MULTIPOLYGON(((30 10,10 20,20 40,40 40,30 10),(60 40,40 50,50 70,70 70,60 40)))

thank you

@destus90
Copy link
Contributor

@arthur-e can you publish the latest version of this library on npm? When I try to install it, I get the source code without the changes, which were made in a pull request (#136).

@arthur-e
Copy link
Owner

@destus90 Oops, the version number in package.json was lagging. Sorry! 1.3.4 should be available now.

@theca-cto
Copy link

theca-cto commented Mar 26, 2021

Still seems not to work for multipolygons or polygons with holes. The following hack-round seems to have dealt with this temporarily, for the purposes of creating a WKT string, and may (perhaps?) be helpful to others and/or provide some hints as to what's up:

function wktfromlatlngsArr(latlngArr,LeafletObject) { //Returns a wkt string from a latlng sub-array. We can assemble these into multipolygons and polygons with holes
      //create a compatibility object to allow for Wicket relying on out-of-date Leaflet
      const compatible = {
        _latlngs: [latlngArr],
        getLatLngs: () => {
          return [latlngArr];
        }
      }
      compatible.constructor = LeafletObject.constructor;
      var wkt = new Wkt.Wkt();
      return wkt.fromObject(compatible).write();
    }

 function wktfromLeafletObject(LeafletObject) {
      let wktstr="";
      if(Array.isArray(LeafletObject.getLatLngs()[0][0])) { //It's a MultiPolygon
        let innerpolys=[];
        LeafletObject.getLatLngs().forEach(function(itm){ //for each component polygon
          let polyrings=[];
          itm.forEach(function(llarr){ let polywkt=this.wktfromlatlngsArr(llarr,LeafletObject).substr(8); polyrings.push(polywkt.substr(0,(polywkt.length-1))); }.bind(this)); //For each of the polygon's rings, get just coordinates in single parentheses
          innerpolys.push("("+polyrings.join(',')+")");
          }.bind(this));
        wktstr="MULTIPOLYGON("+innerpolys.join(',')+")";
      } else { //a single Polygon
        let polyrings=[];
        LeafletObject.getLatLngs().forEach(function(llarr){ let polywkt=this.wktfromlatlngsArr(llarr,LeafletObject).substr(8); polyrings.push(polywkt.substr(0,(polywkt.length-1))); }.bind(this)); //For each of the polygon's rings, get just coordinates in single parentheses
        wktstr="POLYGON("+polyrings.join(',')+")";
      }
      return wktstr;
    }

let wktpolystr=this.wktfromLeafletObject(polygon);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants