polygon.contains and polygon.getExtent()?

1311
5
Jump to solution
03-09-2018 11:47 AM
ShaningYu
Frequent Contributor

I created a polygon from ar2D. 

var polygon_ = new esri.geometry.Polygon({

  "rings": ar2D,

  "spatialReference": {

  "wkid": 102100

 }

});

var extent = polygon_.getExtent();

console.log("polygonExtent", extent);

var bContains = polygon_.contains(new esri.geometry.Point( ar2D[0] ));

------------------

However, the Results show:

the values of extent.xmax,extent.xmin,extent.ymax,extent.ymin are NaN

bContains = false

Unknwon why?  Thanks if you can help.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JohnGrayson
Esri Regular Contributor

If ar2D[0] represents a point, then it wouldn't make sense to use ar2D as the 'rings' of a polygon. Considering that the extent coordinates are NaN, my guess is that ar2D needs to be wrapped up in an array like so:

var polygon_ = new esri.geometry.Polygon({

"rings": [ar2D],

"spatialReference": {

"wkid": 102100

}

});

...but just guessing as we have no idea what ar2D is...

View solution in original post

5 Replies
RobertScheitlin__GISP
MVP Emeritus

Shaning,

  Do your points in your array have a spatial reference assigned?

DanPatterson_Retired
MVP Emeritus

Vince Angelo‌ asked a similar question about one of Shaning's other posts with no response yet

https://community.esri.com/thread/211013-polygoncontans-point-returns-false

JohnGrayson
Esri Regular Contributor

If ar2D[0] represents a point, then it wouldn't make sense to use ar2D as the 'rings' of a polygon. Considering that the extent coordinates are NaN, my guess is that ar2D needs to be wrapped up in an array like so:

var polygon_ = new esri.geometry.Polygon({

"rings": [ar2D],

"spatialReference": {

"wkid": 102100

}

});

...but just guessing as we have no idea what ar2D is...

ShaningYu
Frequent Contributor

Revised the code as

var ar2D = new Array();

for (var j = 0; j < p_.length; j++) {

   var ar1D = [p_[0], p_[1]];

   ar2D.push(ar1D);

} var polygon = new esri.geometry.Polygon(new esri.SpatialReference( { wkid: 102100 } ) );

polygon.addRing( ar2D );

var pt = new esri.geometry.Point(ar2D[0], new esri.SpatialReference({ wkid: 102100 }));

Then  polygon.contains(pt)   returns true.

Thanks to Robert, Dan and John.

RobertScheitlin__GISP
MVP Emeritus

Shaning,

   Don't forget to mark this question as answered by clicking on the "Mark Correct" link on the reply that answered your question.

0 Kudos