How to add feature attribute value

4177
3
Jump to solution
03-20-2015 06:04 AM
KK2014
by
New Contributor III

I want to create o point feature and add string value to string attribute (ex:"description") in offline status and then sync to server.In my example I can create point feature and I give value to "description" attribute and then I give parameter in addFeature( ).In online mode my new point feature geometry created but my "description" attribute value is empty.How can I record my attribute value(ex:"Hi").?My code is below:

Thanks.

// Copyright 2014 ESRI

//

// All rights reserved under the copyright laws of the United States

// and applicable international laws, treaties, and conventions.

//

// You may freely redistribute and use this sample code, with or

// without modification, provided you include the original copyright

// notice and use restrictions.

//

// See the Sample code usage restrictions document for further information.

//

import QtQuick 2.2

import QtQuick.Controls 1.2

import ArcGIS.Runtime 10.3

import ArcGIS.Extras 1.0

ApplicationWindow {

    id: appWindow

    width: 800

    height: 600

    title: "OfflineTest"

     property double scaleFactor: System.displayScaleFactor

     property int fontSize: 15 * scaleFactor

     property bool isOnline: true

     property string featuresUrl: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Sync/WildfireSync/FeatureServer"

     property string gdbPath: "~/ArcGIS/Runtime/Data/Test/offlineSample.geodatabase"

     property var selectedFeatureId: null

     Envelope {

         id: sfExtent

         xMin: -13643665.582273144

         yMin: 4533030.152110769

         xMax: -13618899.985108782

         yMax: 4554203.2089457335

     }

     Map {

         id: mainMap

         anchors {

             left: parent.left

             top: parent.top

             right: parent.right

             bottom: msgRow.top

         }

        extent: sfExtent

         ArcGISTiledMapServiceLayer

         {

              url: "http://services.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer"

         }

         FeatureLayer {

             id: onlineLayer

             featureTable: featureServiceTable

             Component.onCompleted: {

                 renderer = onlineRenderer

             }

         }

         FeatureLayer {

             id: offLineLayer

             featureTable: GeodatabaseFeatureTable {

                 geodatabase: gdb.valid ? gdb : null

                 featureServiceLayerId: 0

             }

             selectionColor: "cyan"

         }

         onMouseClicked: {

             var featureJson = {

                             geometry: {

                             x: mouse.mapX,

                             y: mouse.mapY,

                             spatialReference: mouse.mapPoint.spatialReference

                                    },

                                   attributes:

                                   {    

                                       description: "Hi"

                                   }

                               }

                               var feature = offLineLayer.featureTable.addFeature(featureJson);

                                syncButton.enabled = true;

                                statusText.text = "Tap on Sync to update the Feature Service with the edits";

         }

     }

     SimpleRenderer {

         id: onlineRenderer

         symbol: SimpleMarkerSymbol {

             style: Enums.SimpleMarkerSymbolStyleCircle

             color: "red"

             size: 7

         }

     }

     GeodatabaseFeatureServiceTable {

         id: featureServiceTable

         url: featuresUrl + "/0"

     }

     ServiceInfoTask {

         id: serviceInfoTask

         url: featuresUrl

         onFeatureServiceInfoStatusChanged: {

             if (featureServiceInfoStatus === ServiceInfoTask.FeatureServiceInfoComplete) {

                 statusText.text = "Service info received";

                 generateButton.enabled = true;

             } else if (featureServiceInfoStatus === ServiceInfoTask.featureServiceInfoError) {

                 statusText.text = "Error:" + errorString;

                 generateButton.enabled = false;

                 cancelButton.text = "Start Over";

             }

         }

     }

     Feature {

         id: featureToEdit

     }

     Point {

         id: newPoint

         spatialReference: mainMap.spatialReference

     }

     Rectangle {

         anchors {

             fill: controlsColumn

             margins: -10 * scaleFactor

         }

         color: "lightgrey"

         radius: 5 * scaleFactor

         border.color: "black"

         opacity: 0.77

     }

     Column {

         id: controlsColumn

         anchors {

             left: parent.left

             top: parent.top

             margins: 20 * scaleFactor

         }

         spacing: 7

         Button {

             text: "Generate Geodatabase"

             id: generateButton

             enabled: false

             onClicked: {

                 generateGeodatabaseParameters.initialize(serviceInfoTask.featureServiceInfo);

                 generateGeodatabaseParameters.extent = mainMap.extent;

                 generateGeodatabaseParameters.returnAttachments = true;

                 statusText.text = "Starting generate geodatabase task";

                 geodatabaseSyncTask.generateGeodatabase(generateGeodatabaseParameters, gdbPath);

                 busyIndicator.visible = true;

             }

         }

         Button {

             id: syncButton

             text: "Sync"

             width: generateButton.width

             enabled: false

             onClicked: {

                 enabled = false;

                 geodatabaseSyncTask.syncGeodatabase(gdb.syncGeodatabaseParameters, gdb);

                 busyIndicator.visible = true;

                 statusText.text = "Starting sync task";

             }

         }

         Button {

             id: cancelButton

             text: "Cancel"

             width: generateButton.width

             enabled: false

             onClicked: {

                 geodatabaseSyncTask.cancelJob(syncStatusInfo);

                 enabled = false;

                 text = "Cancel";

             }

         }

         Row {

             id: toggleOnlineOffline

             spacing: 10

             Text {

                 id: onlineStatus

                 text: appWindow.isOnline ? "  Online " : "  Offline "

                 color: "black"

             }

             Switch {

                 id: switchToggle

                checked: appWindow.isOnline

                 enabled: false

                 onCheckedChanged: {

                     appWindow.isOnline = checked;

                     if (checked === true && Enums.GenerateStatusCompleted)

                         statusText.text = "Switch to Offline Mode to continue editing.";

                     else if (checked === false && Enums.GenerateStatusCompleted)

                         statusText.text = "Select a feature.";

                 }

             }

         }

     }

     Geodatabase {

         id: gdb

         path: geodatabaseSyncTask.geodatabasePath

         onValidChanged: {

             if (valid) {

                 var gdbtables = gdb.geodatabaseFeatureTables;

                 for(var i in gdbtables) {

                     console.log (gdbtables.featureServiceLayerName);

                 }

             }

         }

     }

     GeodatabaseSyncStatusInfo {

         id: syncStatusInfo

     }

     GeodatabaseSyncTask {

         id: geodatabaseSyncTask

         url: featuresUrl

         onGenerateStatusChanged: {

             statusText.text = generateStatus;

             if (generateStatus === GeodatabaseSyncTask.GenerateComplete) {

                 statusText.text = geodatabasePath;

                 cancelButton.enabled = false;

                 isOnline = false;

                 busyIndicator.visible = false;

                 generateButton.enabled = false;

             } else if (generateStatus === GeodatabaseSyncTask.GenerateError) {

                 statusText.text = "Error: " + generateGeodatabaseError.message + " Code= "  + generateGeodatabaseError.code.toString() + " "  + generateGeodatabaseError.details;

                 generateButton.enabled = false;

                 cancelButton.text = "Start Over";

             }

         }

         onGeodatabaseSyncStatusInfoChanged: {

             if (geodatabaseSyncStatusInfo.status === Enums.GeodatabaseStatusUploadingDelta) {

                 var deltaProgress = geodatabaseSyncStatusInfo.deltaUploadProgress/1000;

                 var deltaSize = geodatabaseSyncStatusInfo.deltaSize/1000;

                 statusText.text = geodatabaseSyncStatusInfo.statusString + " " + String(deltaProgress) + " of " + String(deltaSize) + " KBs...";

             } else

                 statusText.text = geodatabaseSyncStatusInfo.statusString + " " + geodatabaseSyncStatusInfo.lastUpdatedTime.toString() + " " + geodatabaseSyncStatusInfo.jobId.toString();

             if (geodatabaseSyncStatusInfo.status !== GeodatabaseSyncStatusInfo.Cancelled)

                 cancelButton.enabled = true;

             syncStatusInfo.json = geodatabaseSyncStatusInfo.json;

         }

         onSyncStatusChanged: {

             featureServiceTable.applyFeatureEdits();

             featureServiceTable.refreshFeatures();

             if (syncStatus === GeodatabaseSyncTask.SyncComplete) {

                 cancelButton.enabled = false;

                 syncButton.enabled = false;

                 statusText.text = "Sync completed."

                 busyIndicator.visible = false;

                 switchToggle.enabled = true;

                 var errorString = "";

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

                     var error = syncErrors.featureEditErrors;

                     errorString += "\nLayer Id: " + error.layerId + "\nObject Id: " + error.objectId + "\nGlobal Id: " + error.globalId + "\nEdit operation: " + error.editOperationString + "\nError: " + error.error.description;

                 }

             }

             if (syncStatus === GeodatabaseSyncTask.SyncError)

                 statusText.text = "Error: " + syncGeodatabaseError.message + " Code= "  + syncGeodatabaseError.code.toString() + " "  + syncGeodatabaseError.details;

         }

     }

     GenerateGeodatabaseParameters {

         id: generateGeodatabaseParameters

     }

     Rectangle {

         anchors {

             fill: msgRow

             leftMargin: -10 * scaleFactor

         }

         color: "lightgrey"

         border.color: "black"

         opacity: 0.77

     }

     Row {

         id: msgRow

         anchors {

             bottom: parent.bottom

             left: parent.left

             leftMargin: 10 * scaleFactor

             right: parent.right

         }

         spacing: 10 * scaleFactor

         BusyIndicator {

             id: busyIndicator

             anchors.verticalCenter: parent.verticalCenter

             enabled: false

             visible: enabled

             height: (parent.height * 0.5) * scaleFactor

             width: height * scaleFactor

         }

         Text {

             id: statusText

             anchors.bottom: parent.bottom

             width: parent.width

             wrapMode: Text.WordWrap

             font.pixelSize: fontSize

         }

     }

     Rectangle {

         anchors.fill: parent

         color: "transparent"

         border {

             width: 0.5 * scaleFactor

             color: "black"

         }

     }

     Component.onCompleted: {

         statusText.text = "Getting service info";

         serviceInfoTask.fetchFeatureServiceInfo();

     }

}

0 Kudos
1 Solution

Accepted Solutions
JeanneTrieu
Occasional Contributor

HI KK,

This is how you will need to change your code to make it work with the latest example:

var featureJson = {

                "attributes" :
                {
                    "description": "hi"
                },

                "geometry": {
                    "x": mouse.mapX,
                    "y": mouse.mapY,
                    "spatialReference": {

                        "wkid": mainMap.spatialReference.wkid
                    }
                }
            }

            var feature = offLineLayer.featureTable.addFeature(featureJson);

Regards,

Jen Trieu | Product Eng.

View solution in original post

3 Replies
EricBader
Occasional Contributor III

KK,

First, I would suggest first that you try your code with 10.2.5, which has now been released.

I was able to refactor the code you shared and it seemed to work ok.

Let us know! Thanks

KK2014
by
New Contributor III

Thanks,

Solved the problem after converted to 10.2.5.

0 Kudos
JeanneTrieu
Occasional Contributor

HI KK,

This is how you will need to change your code to make it work with the latest example:

var featureJson = {

                "attributes" :
                {
                    "description": "hi"
                },

                "geometry": {
                    "x": mouse.mapX,
                    "y": mouse.mapY,
                    "spatialReference": {

                        "wkid": mainMap.spatialReference.wkid
                    }
                }
            }

            var feature = offLineLayer.featureTable.addFeature(featureJson);

Regards,

Jen Trieu | Product Eng.