Extracting data from a Shapefile

207
3
Jump to solution
09-01-2021 07:02 AM
Jean-Philippe
New Contributor

Hello all,

I am using the runtime SDK for Qt to display the content of a shapefile on a map. I am first creating a ShapefileFeatureTable from the shapefile, and then a FeatureLayer.

ShapefileFeatureTable* featureTable = new ShapefileFeatureTable(dataPath, this);
FeatureLayer* layer = new FeatureLayer(featureTable, this);

 All works correctly and I see the region boundaries contained in the shapefile when I append the layer to the map. I can also choose the type of renderer to display the data.

What I want to do is find the center or centroid of each of these regions, maybe using the Envelope returned from an extent() function, and then connect different center points with custom polylines.

The problem is that I don't know how to get to the drawn data (points/polylines/polygons) contained in the shapefile. I tried using member functions from the FeatureTable class with no success. For example FeatureTable::numberOfFeatures() returns 0, and FeatureTable::fields() returns an empty list.

How can I access the region boundaries data that is drawn to be able to process it?

If possible, I would like to to everything in the runtime SDK.

Thank you very much for your help!

Jean-Philippe

0 Kudos
1 Solution

Accepted Solutions
JamesBallard1
Esri Contributor

@Jean-Philippe ,

   It sounds like you're on the right track. It's suspicious that numberOfFeatures would return 0 if that feature table is rendering data. ShapefileFeatureTable is a Loadable object, so before running any async tasks with it or checking the number of features, you must make sure it's loaded first. Please confirm if the table is loaded and then we can troubleshoot further.

Here's an example.

  ShapefileFeatureTable* table = new ShapefileFeatureTable("/path/to/myData.shp", this);

  connect(table, &ShapefileFeatureTable::doneLoading, this, [table](Error e)
  {
    if (!e.isEmpty())
    {
      qDebug() << "table failed to load:" << e.message() << e.additionalMessage();
      return;
    }
    qDebug() << table->numberOfFeatures();
  });

When you visualize the table data with a FeatureLayer, that will automatically load the table so it can be displayed.

View solution in original post

0 Kudos
3 Replies
JamesBallard1
Esri Contributor

@Jean-Philippe ,

   It sounds like you're on the right track. It's suspicious that numberOfFeatures would return 0 if that feature table is rendering data. ShapefileFeatureTable is a Loadable object, so before running any async tasks with it or checking the number of features, you must make sure it's loaded first. Please confirm if the table is loaded and then we can troubleshoot further.

Here's an example.

  ShapefileFeatureTable* table = new ShapefileFeatureTable("/path/to/myData.shp", this);

  connect(table, &ShapefileFeatureTable::doneLoading, this, [table](Error e)
  {
    if (!e.isEmpty())
    {
      qDebug() << "table failed to load:" << e.message() << e.additionalMessage();
      return;
    }
    qDebug() << table->numberOfFeatures();
  });

When you visualize the table data with a FeatureLayer, that will automatically load the table so it can be displayed.

View solution in original post

0 Kudos
Jean-Philippe
New Contributor

Thank you James. What you suggested was the key!

I was calling the FeatureTable functions before the table was loaded. If I wait for the FeatureTable::doneLoading signal to be emitted, it works.

I can then call the FeatureTable::queryFeatures function, and connect the FeatureTable::queryFeaturesCompleted signal to a slot where I iterate over the list of features contained in the FeatureQueryResult object. From each feature I get the geometry, from which I can get the Envelope and the center coordinates.

Now I will look into connecting some of these points in pair with polylines to create flow maps.

0 Kudos
JamesBallard1
Esri Contributor

That's great to hear, @Jean-Philippe . We're always happy to help, so don't hesitate to reach out with any other questions.

0 Kudos