i want to calculate centroid (center point ) of a polygon.
Solved! Go to Solution.
Thanks rajni sharma - I see what you are trying to do. This works for me:
const ringJson = [[["172.4843305878348","-43.48191185124988"],["172.4877698503442","-43.48248171686965"],["172.48715361282302","-43.484065878559875"],["172.48428867831893","-43.48421950013839"],["172.4843305878348","-43.48191185124988"]]]
var boundaryJson = {"rings":ringJson, "spatialReference":sr};
var boundary= ArcGISRuntimeEnvironment.createObject("Polygon", {json: boundaryJson});
console.log("boundary", boundary)
console.log(JSON.stringify(boundary.json));
var centroid = GeometryEngine.labelPoint(boundary);
console.log("==========");
console.log("centroid", centroid);
console.log(JSON.stringify(centroid.json));
For reference, when you see "qml point sum hex number" you are printing the address of the Point object. To see the details of the point you need to either print property values or stringify the json:
console.log(JSON.stringify(centroid.json));
Hi rajni sharma - you can use the GeometryEngine.labelPoint to give you a point that's within the polygon and near the center of gravity.
how to use it , is their any example i could follow, because i could not found any example for it.
Should i use it like this:
var a = Point labelPoint(Polygon ring);
it gives some syntax error
We have a number of samples that cover the use of GeometryEngine on github - for example look at this one.
For the label point call you should do something like:
var centroid = GeometryEngine.labelPoint(ring);
hi Luke Smallwood
I tried it your way, but when i try to console the qml point it crashed my application everytime.
Actually i am trying to put a picture marker on centre of polygon.
So i need to get centre x and y coordinates
Suppose this is the JSON m passing in ring
[[["172.4843305878348","-43.48191185124988"],["172.4877698503442","-43.48248171686965"],["172.48715361282302","-43.484065878559875"],["172.48428867831893","-43.48421950013839"],["172.4843305878348","-43.48191185124988"]]]
function createPolygon(ring) {
// create polygon using json
const sr = { "wkid": 4326};
var boundaryJson = {"rings":ring,"spatialReference":sr};
var boundary= ArcGISRuntimeEnvironment.createObject("Polygon", {"json": boundaryJson,"id":"property"});
var centroid = GeometryEngine.labelPoint(boundary);
console.log("==========");
console.log(JSON.stringify(centroid));//Crashed my application
return boundary;
}
Hi rajni sharma - I think this line is causing the problems because the returned Polygon will be null:
var boundary= ArcGISRuntimeEnvironment.createObject("Polygon", {"json": boundaryJson,"id":"property"});
I think this should be:
var boundary= ArcGISRuntimeEnvironment.createObject("Polygon", {json: boundaryJson});
(I'm not sure what the "id" : "property" is for here?)
my application still crashed. but when i do not stringify it, it return qml point sum hex number
const sr = { "wkid": 4326};
var boundaryJson = {"rings":ring,"spatialReference":sr};
var boundary= ArcGISRuntimeEnvironment.createObject("Polygon", {"json": boundaryJson});
var centroid = GeometryEngine.labelPoint(boundary);
console.log("==========");
console.log((centroid));
return boundary;
Thanks rajni sharma - I see what you are trying to do. This works for me:
const ringJson = [[["172.4843305878348","-43.48191185124988"],["172.4877698503442","-43.48248171686965"],["172.48715361282302","-43.484065878559875"],["172.48428867831893","-43.48421950013839"],["172.4843305878348","-43.48191185124988"]]]
var boundaryJson = {"rings":ringJson, "spatialReference":sr};
var boundary= ArcGISRuntimeEnvironment.createObject("Polygon", {json: boundaryJson});
console.log("boundary", boundary)
console.log(JSON.stringify(boundary.json));
var centroid = GeometryEngine.labelPoint(boundary);
console.log("==========");
console.log("centroid", centroid);
console.log(JSON.stringify(centroid.json));
For reference, when you see "qml point sum hex number" you are printing the address of the Point object. To see the details of the point you need to either print property values or stringify the json:
console.log(JSON.stringify(centroid.json));
Thanks it worked. I was trying to stringify it, but didn't know, i have to stringify the json property of the point. Thanks a lot again