copy feature from one layer to another

3980
7
Jump to solution
01-14-2016 12:29 AM
cadgism
Occasional Contributor

I have two feature  layers layer1 & layer2. I want to copy a feature from layer1 to layer2  and chnage its attributes as per layer2. I had already created a graphic from layer1 and how can i copy this to layer2 please ???

0 Kudos
1 Solution

Accepted Solutions
cadgism
Occasional Contributor

Solved. The problem was i was not using proxy. 

View solution in original post

0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

cadgis,

   Is Layer2 a FeatureService? You can only edit features using a FeatureService (i.e. FeatureServer instead of MapServer in the url). If it is then you can use applyEdits

0 Kudos
cadgism
Occasional Contributor

Thank you Robert Scheitlin..

         

      Yes,  Layer2 is published as  Featureservice.  What I am trying to do is select the feature from Layer1 as graphic, and trying to do ApplyEdit (graphic to Layer2) is this the right way? But it fails. I meanLayer2 is not updated in the Geodatabase.

0 Kudos
GirishYadav
Occasional Contributor

cadgis m,

Are you passing the graphic object directly to ApplyEdits? I guess that would cause it not to save because If you directly fetch feature out from FeatureLayer then it has few properties that are referenced to its parent feature layer.

Try to get the JSON from the graphic and create a new Graphic using that JSON and save this new graphic to Layer2.

var newFeature = new Graphic(graphic.toJson());

featureLayer.applyEdits([newFeature], null, null)

you can also update the attributes of the newFeature before applying edits.

-Girish

cadgism
Occasional Contributor

Thank you Girish Yadav

  Oh yes i passing the graphic object directly to ApplyEdits .

I will try the JSON thing and come back.

0 Kudos
cadgism
Occasional Contributor

Hi Grish..

I tried to as you said the following way, there is no error but it simple does not work meaning the feature is not saved in the Geodatabase.  Can you please check...

for (var i = 0; i < results.features.length; i++) {

        //Create the graphic for the parcels

        var graphic = results.features;

        graphic.setSymbol(symbol);

        app.map.graphics.add(graphic);

        app.map.setExtent(graphic.geometry.getExtent().expand(2));

        var schAttrib = " " + graphic.attributes.PIN;

        var schTextSymbol = new esri.symbol.TextSymbol(schAttrib);

        schTextSymbol.setColor(new dojo.Color([255, 0, 0]));

        var schFont = new esri.symbol.Font("9pt",

        esri.symbol.Font.STYLE_NORMAL,

        esri.symbol.Font.VARIANT_NORMAL,

        esri.symbol.Font.WEIGHT_BOLD, "Arial");

        //text symbol for the PIN

        schTextSymbol.setAlign(esri.symbol.TextSymbol.ALIGN_MIDDLE);

        schTextSymbol.setFont(schFont);

        var pt = graphic.geometry.getExtent().getCenter();

        var gra = new esri.Graphic(pt, schTextSymbol);

        app.map.graphics.add(gra);

        app.map.removeLayer(cadastrePlotLayer);

     

      

         var newFeature = new esri.Graphic(graphic.toJson());

         ladpTempLayer.applyEdits([newFeature], null, null)

  

    }

0 Kudos
GirishYadav
Occasional Contributor

Hi cadgis m,

Sorry for the late reply, I saw this post just now. I don't know whether you have figured out the solution or not, but as this question is not yet marked as answered, I will put in my thoughts...

Is the Spatial reference of the input Graphic same as the spatial reference of Layer 2?

looks like you are making a query to Layer1 and inserting the returned graphic into Layer2. One thing to remember here is that the query does take "outSR" param as input and if its not specified then the returned graphics will be in Map SR (mostly 102100). And  if Layer2 has different SR then map's SR then the graphic will get added into the layer but it will be displaced (or not visible).

Therefore, either

1. reproject graphics geom into Layer2's SR and then do ApplyEdits OR

2. set "outSR" into your query to match Layer2's SR, (but in  this case adding graphic to map will not show graphic to the correct place).

-Girish

0 Kudos
cadgism
Occasional Contributor

Solved. The problem was i was not using proxy. 

0 Kudos