Reading .dbf data from a Shapefile

2842
3
Jump to solution
11-29-2020 10:41 AM
MarylynAlaso
New Contributor

I apologize in advance of this is a basic question.

I am trying to access the data attached to a shapefile (.dbf data). I am able to read and display the map using a ShapefileFeatureTable. 

I want to be able to read the .dbf data. The shapefile am using is LondonBorroughs.shp (https://github.com/Robinlovelace/Creating-maps-in-R/tree/master/data). It contains info such as "name" and "Pop_2001" (the population in 2001).

I want to - for instance - click on a map feature and extract the name and population for that specific feature.

Any assistance would be highly appreciated.

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
MarkBaird
Esri Regular Contributor

@MarylynAlaso I can see you have posted this question in the ArcGIS Runtime for Java area so I'm assuming you are writing a JavaFX application.

When you are working with any types of features regardless on if it comes from OGC data, feature layers, or in your case shapefiles, you can perform a programatic "identify" operation.  This basically allows you to get geometry and attribute information from the item you have just clicked on.

I've not used your specific data, but I've built upon an existing samples we have in git:

https://github.com/Esri/arcgis-runtime-samples-java/tree/master/feature_layers/feature-layer-shapefi...

I've added some extra code to listen into mouse click events and use that point to perform an identify operation.  In its current form, the code just outputs text to the console, but it should help you to develop the next stage of your application.

// add the feature layer to the map
map.getOperationalLayers().add(featureLayer);

// listen into clicks and identify results
mapView.setOnMouseClicked(event -> {
System.out.println("clicked");
Point2D clickedPoint = new Point2D(event.getX(), event.getY());

// listenable future for waiting for results from identify on layer
ListenableFuture<IdentifyLayerResult> resultFuture = mapView.identifyLayerAsync(featureLayer,clickedPoint,10,false);
resultFuture.addDoneListener(() -> {
System.out.println("got result");

try {
// get the results from the future
IdentifyLayerResult result = resultFuture.get();

// loop through each element (probably only 1) and get the attributes
for (GeoElement element : result.getElements()) {
Map<String, Object> attributes = element.getAttributes();
System.out.println("attributes " + attributes);
}

} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
});

 Does this help?

View solution in original post

3 Replies
DanPatterson
MVP Esteemed Contributor

Why not add the shapefile to an empty map so you have a layer in the map's table of contents.

Right-click on the layer and open its attribute table


... sort of retired...
0 Kudos
BenCoubrough
Esri Contributor

You can read .dbf files with programs such as MS Excel. You should not attempt to edit the file outside of ArcGIS Desktop/ArcGIS Pro however because it can corrupt the data.

The supported method of viewing DBF data is in ArcGIS Desktop/ArcGIS Pro by loading the Shapefile into your map and then Right clicking on the layer in your Table of Contents and then opening the Attribute Table.

To open the DBF file in MS Excel, Go to Open > Navigate to where the DBF file is then select open.

You will need to make sure the All Files or DBF Files file filter is on however.

2020-11-30_9-04-56.png

 

0 Kudos
MarkBaird
Esri Regular Contributor

@MarylynAlaso I can see you have posted this question in the ArcGIS Runtime for Java area so I'm assuming you are writing a JavaFX application.

When you are working with any types of features regardless on if it comes from OGC data, feature layers, or in your case shapefiles, you can perform a programatic "identify" operation.  This basically allows you to get geometry and attribute information from the item you have just clicked on.

I've not used your specific data, but I've built upon an existing samples we have in git:

https://github.com/Esri/arcgis-runtime-samples-java/tree/master/feature_layers/feature-layer-shapefi...

I've added some extra code to listen into mouse click events and use that point to perform an identify operation.  In its current form, the code just outputs text to the console, but it should help you to develop the next stage of your application.

// add the feature layer to the map
map.getOperationalLayers().add(featureLayer);

// listen into clicks and identify results
mapView.setOnMouseClicked(event -> {
System.out.println("clicked");
Point2D clickedPoint = new Point2D(event.getX(), event.getY());

// listenable future for waiting for results from identify on layer
ListenableFuture<IdentifyLayerResult> resultFuture = mapView.identifyLayerAsync(featureLayer,clickedPoint,10,false);
resultFuture.addDoneListener(() -> {
System.out.println("got result");

try {
// get the results from the future
IdentifyLayerResult result = resultFuture.get();

// loop through each element (probably only 1) and get the attributes
for (GeoElement element : result.getElements()) {
Map<String, Object> attributes = element.getAttributes();
System.out.println("attributes " + attributes);
}

} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
});

 Does this help?