2

I have polygon data as:

POLYGON ((1645481.0939999996 6124506.9885, 1645470.0473999996 6124473.5406, 1645475.7094 6124440.1391,1645481.0939999996 6124506.9885))

in wkid=2193

I want to project this polygon using geometry service with new spatial reference wkid= 3857.

I was able to project point using similar data on new spatial reference wkid=3857, but that is not happening with polygon.

Which method should I use to project polygon using geometry service?

 function addGraphic() {

   var wkt = new Wkt.Wkt(); 
   wkt.read("POLYGON ((1645481.0939999996 6124506.9885, 1645470.0473999996   6124473.5406, 1645475.7094 6124440.1391,1645481.0939999996 6124506.9885))");
  var polygon = wkt.toObject();

  //set the spatialreference
  polygon.SpatialReference = new esri.SpatialReference({wkid: 2193});
  var outSR = new esri.SpatialReference({ wkid: 3857 });
  var gsvc = new esri.tasks.GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

   var projPoly;
   gsvc.project([polygon], outSR, function (outGeom) {

   projPoly = outGeom[0];

   var symbol = new esri.symbol.SimpleFillSymbol().setStyle(esri.symbol.SimpleFillSymbol.STYLE_SOLID);
   var polygonGraphic = new esri.Graphic(projPoly, symbol);
   map.graphics.add(polygonGraphic);

  });
 }
1
  • user esri.symbol.SimpleFillSymbol.STYLE_NULL
    – Ashish
    Sep 15, 2015 at 11:19

1 Answer 1

2

There are two issues that you need to solve.

  • Firstly, the polygon that you have, is in a format that is called the WKT. You will have to convert it to a geometry in the ESRI's JavaScript format. I have used Wicket in the past to do this conversation for me.
  • Once you have an ESRI Polygon, you can use the project operator on the GeometryService. You should make sure that you set the spatialreference on the polygon, before you send it to the service.

You can use code similar to this:

    function projectToWebMercator() {

        var wkt = new Wkt.Wkt();
        wkt.read("POLYGON ((1645481.0939999996 6124506.9885, 1645470.0473999996 6124473.5406, 1645475.7094 6124440.1391,1645481.0939999996 6124506.9885))");
        var polygon = wkt.toObject();
        //set the spatialreference
        polygon.setSpatialReference(new SpatialReference({
                wkid : 2193
            }));

        var outSR = new SpatialReference({
                wkid : 102100
            });
        var gsvc = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
        gsvc.project([polygon], outSR, function (outGeom) {
            console.dir(outGeom); //<-- Do whatever you want with the projected Polygon here
        });
    }
7
  • I've tried to convert WKT to a geometry using arthur-e.github.io/Wicket/sandbox-arcgis.html, but its not showing up anything. Can you please send me some sampple code? I had similar coordinates for point, but when I project them using new spatial reference and geometry service, it worked. Please guide me.
    – Dips
    Oct 6, 2014 at 1:30
  • @Dips: I've added the code. Don't forget to add the two scripts from Wicket in you HTML file. Oct 6, 2014 at 2:05
  • hey..Thanks for the code, but still not getting those two scripts to add from Wicket, please guide me.
    – Dips
    Oct 6, 2014 at 3:26
  • What do you mean when you say 'not getting those scripts' did you download those scripts from github? Oct 6, 2014 at 3:28
  • I've added scripts and modified code as per your directions, but still its not showing up polygon on the map. Please check my above edited code. If I modify polygon data i.e. add more coordinates to it around 40 coordinates, it doesn't call gsvc.project method.
    – Dips
    Oct 6, 2014 at 5:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.