Select to view content in your preferred language

Polygon object references...

801
2
09-08-2010 12:59 PM
BillZborowski1
Emerging Contributor
Hi guys,

Basically what I'm doing is creating a wrapper around the Polygon class to include some extra variables I need for output.  I return query results and read them into a list, then created an ENCODE function to generate a specific JSON format I want to use.

The problem I'm having is that when I store a Polygon in an object list, if I then go back and write out the coordinates of the Polygon, every Polygon in the list writes the value of the LAST polygon retrieved in the search.

So if I run display(feature) it works as expected, if i add to the list and then display later, it takes the LAST polygon's value.  What gives?  This seems counter-intuitive.

ICursor cursor = fclass.ITable_search(qf, true);
IRow row;
   
//create a list.
Collection<AgsFeature> features = new LinkedList<AgsFeature>();
   
//iterate through the query results and add them to the FEATURECOLLECTION.
while ((row = cursor.nextRow()) != null) {
 Polygon currentPoly = new Polygon();
 currentPoly = (Polygon)row.getValue(9);
 switch (currentPoly.getGeometryType()) {
  case esriGeometryType.esriGeometryPolygon:
   //build the geometry.
   AgsGeometry geometry = new AgsGeometry(currentPoly);
   
   //build the feature.
   AgsFeature feature = new AgsFeature(String.valueOf(row.getValue(0)),
              geometry,
              new HashMap<String,Object>());
     
   display(feature);
 
   features.add(feature);
   break;
  default:
   throw new RuntimeException("Not implemented yet.");
  }
}
0 Kudos
2 Replies
BillZborowski1
Emerging Contributor
Hey guys,

Figured I'd reply as I seemingly found a solution.  Still have to do more testing, including stress tests, to validate how effective it is, but here goes.

//iterate through the query results and add them to the FEATURECOLLECTION.
while ((row = cursor.nextRow()) != null) {

 Feature feature1 = (Feature)row;
 Polygon currentPoly = (Polygon)feature1.getShapeCopy();
 //Polygon currentPoly = (Polygon) row.getValue(9);
 switch (currentPoly.getGeometryType()) {
  case esriGeometryType.esriGeometryPolygon:
   //build the geometry.
   AgsGeometry geometry = new AgsGeometry(currentPoly);
     
   //build the feature.
   AgsFeature feature = new AgsFeature(String.valueOf(row.getValue(0)),
              geometry,
              new HashMap<String,Object>());
 
   features.add(feature);
   break;
  default:
   throw new RuntimeException("Not implemented yet.");
  }
}
0 Kudos
JamesMacKay
Deactivated User
Hi Bill,

The reason you have to do that is because of this line:

ICursor cursor = fclass.ITable_search(qf, true);


The "true" parameter (the recycling parameter) indicates that every time a new feature is returned from the geodatabase it overwrites the previous feature in memory. Your feature references were all pointing to the same memory at any one time.

Provided you're just holding the polygons for later use and don't intend on modifying any of these features, keeping things as they are now (with recycling = true and the getShapeCopy method) is probably your best bet.

Cheers,
James
0 Kudos