Developer Discussions

Discuss topics with other Hexagon Geospatial Power Portfolio developers and experts to get the most out of our products.
Showing results for 
Search instead for 
Do you mean 
Reply
Occasional Contributor
Posts: 13
Registered: ‎08-23-2016

Portal SDK Update Features (2015 EP05) - URGENT (For Dec 16)

[ Edited ]

[Edit] - Need an update and not a remove/add

The goal is to update the position of a fleet of +/- 2000 cars on the map.

I would like to know how to use properly the update method of the $GP.features.

I add the cars using the Add GeoJson method. In my GeoJson, I have the crs, properties (attributes) and the geometry.

With this I can add my cars on the map.

 

I saw that I need an id field in the geoJson to be able to update the features. But when I add it (like id : 123456), I cannot add the layer. I get a 'Missing feature' error.

What do I need in my geoJson to be able to do this ?

The id filed must precise the real id value of the id field that's in the properties ?

 

Can you give me some tips ?

 

 

ORIGINAL POST : 

Hi,

 

I'm trying to re-use my legend entry to update some features. First I had in maind to remove all the features and to add them again 

I tried different ways : 

  1. Remove Service -> Will remove the service, the legend entry and we won't see the data anymore. Then I recreate the service and everything goes well.. EXCEPT, I get a session resuming (if I try to get the feature info) which I want to avoid.

 

function RemoveService(LayerName) {
    var featureClassId = LayerName + "_ClassID";
    var PSSConfig = GetGeoJsonSDKConfig(LayerName, featureClassId, null);
    $GP.edit.PSS.find(
        PSSConfig,
        function (PSSFound) {
            //PSSFound.service.remove();
            $GP.edit.PSS.remove({ name: LayerName })
        },
        function (PSSNotFound) {
            $GP.ui.info("PSS Not found");
        }
    );
}

2. Remove Legend Entry

Works also, will remove the legend entry. BUT, if I had a layer with same name (featureClassId, etc..), the feature that was i the 'old' legend entry' are back again. And the point is to remove them before adding new features.

function RemoveLayer(LayerName) {
    var featureClassId = LayerName + "_ClassID";
    var PSSConfig = GetGeoJsonSDKConfig(LayerName, featureClassId, null);
    $GP.edit.PSS.find(
        PSSConfig,
        function (PSSFound) {
            var configFindLegend = {
                name: featureClassId
            };
            $GP.legend.find(
				configFindLegend,
				function (LegendFound) {
				    for (var i = 0; i < LegendFound.legendItems.length; i++) {
				        LegendFound.legendItems[i].remove();
				    }
				},
				function (LegendNotFound) {
				    $GP.ui.info("Legend Not found");
				}
			);
        },
        function (PSSNotFound) {
            $GP.ui.info("PSS not found");
        }
    );
}

3. I try then to remove directly all the features in the layer. but there is no way to get all the features using only the featureClassID.

So I added a method to save all the feature Ids that I add to the map.

function returnAddFeature(event, LayerName) {
	// Save Feature Ids
    for (var i = 0; i < event.features.length; i++) {
        featureIdList.push(event.features[0].get_id());
    }
}

Once I want to remove them, I look for them using the find method. But here, I get nothing. 

I even don't enter the succes nor the error callback.

 

var featureIdList = [];
function RemoveFeatures(LayerName, FeatureIdsList) {
    var featClassId = LayerName + "_ClassID";
    var FeatureId = featureIdList[0];
    var featConfig = {
        featureClassId: featClassId,
        featureIds: [FeatureId]
    };
    // Way 1
    $GP.edit.features.remove(featConfig,
        function (removed) {
            $GP.ui.info(removed.toString());
        },
        function (Error) {
            $GP.ui.info("Error");
        });
    // Way 2
    $GP.edit.features.find(
        featConfig,
        function (Features) {
            for (var i = 0; i < Features.features.lenght; i++) {
                Features.features.remove();
            }
        },
        function (NotFound) {
            $GP.ui.info("Features not found");
        }
    );
}

So, can someone tell me how I can do this ? because, really, I'm lost there..

 

Many thanks,

 

David

Technical Evangelist
Posts: 1,131
Registered: ‎09-11-2015

Re: Portal SDK Update Features (2015 EP05) - URGENT (For Dec 16)

Hi David,

 

Considering the fact that you want to dynamically monitor something I'd say that using legend and feature classes is too robust for this task. Why don't you use $GP.map.draw methods that accept GeoJSONs too? Drawings can be simply cleared using $GP.map.draw.clear()

 

Regards,

Jan

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial

Technical Evangelist
Posts: 1,131
Registered: ‎09-11-2015

Re: Portal SDK Update Features (2015 EP05) - URGENT (For Dec 16)

[ Edited ]

You can simply draw a geojson layer like this:

var geoJson = {..geojson content...}
$GP.map.draw.geojson(
   geoJson, 
   function(success) { 
     console.log(success) 
   }, 
   function(err) {
     console.log(err)
   }
);

 

Jan

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial

Occasional Contributor
Posts: 13
Registered: ‎08-23-2016

Re: Portal SDK Update Features (2015 EP05) - URGENT (For Dec 16)

Hi, 

 

Thanks for the answer but the point is to ba able to have it in the legend and to be able to activate / desactivate the layer.

So I need to put everything in the legend + feature Class.

Technical Evangelist
Posts: 1,131
Registered: ‎09-11-2015

Re: Portal SDK Update Features (2015 EP05) - URGENT (For Dec 16)

Hi David,

 

My understanding of the $GP.edit.features.update() method is that it will only update the existing features by a new GeoJSON which has one additional attribute "id". This id must match already existing features on the map.

 

Both add() and update() functions use the same internal function editFeature() which checks if there's any ID property attached to any of the GeoJSON elements. You must either have this ID on all elements OR on none, nothing between.

 

Look for editFeature() function for more details: http://demo.hexagongeospatial.com/communityportal/docs/source/API.html#$GP-edit-features-method-upda...

 

When you're telling that the $GP.edit.features.remove() does not work as expected, are you certain that a feature was found by the ID? Did you check the ID array content?

 

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial

Occasional Contributor
Posts: 13
Registered: ‎08-23-2016

Re: Portal SDK Update Features (2015 EP05) - URGENT (For Dec 16)

In fact, I'm not able to add the first time a geoJson with additional attribute id for each of the contained features.

So there is nothing on my map and I use the add method with my geoJson (with id attribute). I enter the success callback of the addfeature but when I'm back on the map, I get an error 'Missing feature'.

 

I guess that if I'm not able to add data with id, i'll not be able to update. So the first step is to be able to add it.

 

Maybe the issue is I'm trying with a SDK 2015 EP04 and not EP05 ?

Do I need to add an attribute for the PSS configuration ?

 

My geoJson looks like this : 

 

{
	"type":"FeatureCollection",
	"crs":{
		"type":"name",
		"properties":{
			"name":"urn:ogc:def:crs:EPSG::3857"
		}
	},
	"features":[
		{
			"type":"Feature",
			"properties":{
				"Id":"40001",
				"DirectCall":"65845054",
				"Label":"J521",
				"LastUpdDate":"11&#47;9&#47;2016 4:05:17 PM",
				"Plate":"IS2768",
				"Latitude":"49.78599312",
				"Longidute":"5.80835824"
			},
			"geometry":{
				"type":"Point",
				"coordinates":[646583.481621715,6409295.77805155]
			},
			"id":40001
		},
		{
			"type":"Feature",
			"properties":{
				"Id":"40008",
				"DirectCall":"46802853",
				"Label":"G269",
				"LastUpdDate":"11&#47;9&#47;2016 4:05:17 PM",
				"Plate":"WI5621",
				"Latitude":"49.75202659","Longidute":"6.298001263"
			},
			"geometry":{
				"type":"Point",
				"coordinates":[701090.293612554,6403441.45311634]
			},
			"id":40008
		},
		{
			"type":"Feature",
			"properties":{
				"Id":"40010",
				"DirectCall":"23836357",
				"Label":"U743",
				"LastUpdDate":"11&#47;9&#47;2016 4:05:17 PM",
				"Plate":"",
				"Latitude":"49.76036339",
				"Longidute":"6.893747104"
			},
			"geometry":{
				"type":"Point",
				"coordinates":[767408.417274884,6404877.9682653]
			},
			"id":40010
		}
	]
}

 

The PSS Config looks like : 

 

{
	"name":"CarPosition",
	"featureClassDefinitions":[
		{
			"featureClassId":"CarPosition_ClassID",
			"fields":[
				{
				"name":"Id",
				"aliasName":"Id",
				"type":"System.String"
				},
				{
				"name":"DirectCall",
				"aliasName":"DirectCall",
				"type":"System.String"
				},
				{
				"name":"Label",
				"aliasName":"Label",
				"type":"System.String"
				},
				{
				"name":"LastUpdDate",
				"aliasName":"LastUpdDate",
				"type":"System.String"
				},
				{
				"name":"Plate",
				"aliasName":"Plate",
				"type":"System.String"
				},
				{
				"name":"Latitude",
				"aliasName":"Latitude",
				"type":"System.String"
				},
				{
				"name":"Longidute",
				"aliasName":"Longidute",
				"type":"System.String"
				}
			]
		}
	]
}

 

The add works perfectly if I remove the id but doesn't with it.

 

Am I missing Something ?

Technical Evangelist
Posts: 1,131
Registered: ‎09-11-2015

Re: Portal SDK Update Features (2015 EP05) - URGENT (For Dec 16)

[ Edited ]

Please work over Example 14. Editing > Add features to see how it works internally.

 

Unfortunatley you will have to fix it. There is an AJAX call to FormatConverter.WebClient.ashx which is usually not available on the SDK server, so you will have to modify the sample code a little bit to make it working.

 

Then, after feature is added, use callback method to see the IDs (GUID-like) of the generated features. Remember those IDs, then remove all features using function find() in a loop and add new ones, again remembering the new GUIDs.

 

Jan

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial

Technical Evangelist
Posts: 1,131
Registered: ‎09-11-2015

Re: Portal SDK Update Features (2015 EP05) - URGENT (For Dec 16)

[ Edited ]

This code works for me:

var featureClass1 = "FeatureClass1";
var geojson = {
   "type" : "FeatureCollection",
   "features" : [{
         "type" : "Feature",
         "properties" : {
            "Id" : "40001",
            "DirectCall" : "65845054",
            "Label" : "J521",
            "LastUpdDate" : "11&#47;9&#47;2016 4:05:17 PM",
            "Plate" : "IS2768",
            "Latitude" : "49.78599312",
            "Longidute" : "5.80835824"
         },
         "geometry" : {
            "type" : "Point",
            "coordinates" : [646583.481621715, 6409295.77805155]
         }
      }, {
         "type" : "Feature",
         "properties" : {
            "Id" : "40008",
            "DirectCall" : "46802853",
            "Label" : "G269",
            "LastUpdDate" : "11&#47;9&#47;2016 4:05:17 PM",
            "Plate" : "WI5621",
            "Latitude" : "49.75202659",
            "Longidute" : "6.298001263"
         },
         "geometry" : {
            "type" : "Point",
            "coordinates" : [701090.293612554, 6403441.45311634]
         }
      }, {
         "type" : "Feature",
         "properties" : {
            "Id" : "40010",
            "DirectCall" : "23836357",
            "Label" : "U743",
            "LastUpdDate" : "11&#47;9&#47;2016 4:05:17 PM",
            "Plate" : "",
            "Latitude" : "49.76036339",
            "Longidute" : "6.893747104"
         },
         "geometry" : {
            "type" : "Point",
            "coordinates" : [767408.417274884, 6404877.9682653]
         }
      }
   ]
};

ensurePssDisplayed({
   featureClassId : featureClass1,
   name : "PSS1"
}, function (mapServiceId) {
   addFeatures(mapServiceId, featureClass1, geojson);
});

function addFeatures(mapServiceId, featureClassId, geoJson) {
   $GP.edit.features.add({
      mapServiceId : mapServiceId,
      featureClassId : featureClassId,
      geojson : geojson
   }, function (fts) {
      console.log(fts)
   });
}

function ensurePssDisplayed(options, callback) {
   $GP.edit.PSS.find({
      name : options.name
   }, function (ret) {
      $GP.legend.add({
         mapServiceId : ret.mapServiceId,
         names : [options.featureClassId]
      }, function () {
         callback(ret.mapServiceId);
      });
   }, function () {
      $GP.edit.PSS.add({
         name : options.name,
         featureClassDefinitions : [{
               featureClassId : options.featureClassId,
               fields : [{
                     name : "prop1",
                     aliasName : "Property 0",
                     type : "System.String"
                  }
               ]
            }
         ]
      }, function (ret2) {
         $GP.legend.add({
            mapServiceId : ret2.mapServiceId,
            names : [options.featureClassId]
         }, function () {
            callback(ret2.mapServiceId);
         });
      });
   });
}

Note the callback of  $GP.edit.features.add(), in this callback you will find the unique IDs of the added features. That means, you don't assign them manually, they are rather generated and you can reference to them later on.

 

Result on map:

Untitled.png

 

Jan

Jan Neumann
Post Sales Engineer Web Applications
Hexagon Geospatial