Shapefile Reader
Quickstart
The basics
The Shapefile Reader plugin allows you to read data from SHP, SHX, and DBF files. These files formats specifications are available on ESRI website : https://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
Getting started with the Shapefile Reader plugin is really easy. Using Blueprints, after having installed and activated the plugin:
Step 1: Create an actor (or any Blueprint class) and open it
Step 2: Right-click on the graph and select the function Open Shapefile (under the category “ShapefileLib”)
Provide the path of your file as parameter of this function
Step 3: Draw a link from the Handler node, and call Read object
This pure function takes one parameter: the index of the object
Step 4: Get the result of Read object and extract the required data from the object
After following these steps, you should get a graph similar to the one given in the screen capture.
Once your process of the shapefile is done, don’t forget to call Close handler to close the file.
Preventing errors
This code contains two sources of errors that should be avoided.
Firstly, the previous code may fail if there is no shapefile for the given Path. To prevent this, you should always check the return value of the Open Shapefile function: it’s true if the file has been successfully opened, false otherwise.
Secondly, the code may fail if there are no object for the index 0 (i.e. the file is empty). We must know the total number of objects of the shapefile, and ensure that we don’t excess the max index. This can be done by a call to Get Entities Count on the Handler.
After preventing these sources of errors, we have the following graph:
It’s all you need to know to start using the Shapefile Reader to read shapefiles.
Reading DBF files
Now that we know how to read from SHP files, it’s really easy to read from DBF files: it’s the same principle. A screen capture is worth a thousand words, so let’s see it:
As we can see, the main differences are the Open DBF call (instead of Open shapefile), and the access to the data (we’re accessing data of a DBF file instead of data of a SHP file). But globally it’s the same thing.
Advanced
First of all, you need to read a .shp using the open shapefile function
Open Shapefile: this function reads a .shp file at the specified Path. If the read success, the return value will be true. Then you can use the Handler in order to call other functions.
Entities Count: the entities count read in the shapefile.
Max Bound: the max bound vector of the shapefile.
Min Bound: the min bound vector of the shapefile.
Shape Type: the shape type of this shapefile. It can be 2D POINT, 2D ARC, 2D POLYGON, 3D POINT, 3D POLYGON, etc.
Read object: this function reads an object of the shapefile and returns a Shapefile Object. Warning: 0 <= Index < Entities count
Close handler: this function properly closes the shapefile handler. It must be used once the read is done.
Then, you can iterate through Shapefile's objects
Shapefile Type: the shape type of this shapefile's object. It can be 2D POINT, 2D ARC, 2D POLYGON, 3D POINT, 3D POLYGON, etc.
Max Bound: the max bound vector of this object.
Min Bound: the min bound vector of this object.
Shape Id: the shape ID of this object.
Vertices: the list of vertices of this object
Parts: the parts composing this object (in case of multi-parts object). Each part has:
Type: the shape type of this object's part. It can be 2D POINT, 2D ARC, 2D POLYGON, 3D POINT, 3D POLYGON, etc.
Vertices: the list of vertices of this object's part
Close object: this function properly closes this object. It must be used once the read is done.
Finally, you can use DBF file in order to get attributes
Open DBF: this function reads a .dbf file at the specified Path. If the read success, the return value will be true. Then you can use the Handler in order to call other functions.
Close handler: this function properly closes the DBF handler. It must be used once the read is done.
Field count: the number of fields in this DBF file. A field represents an attribute
Record count: the number of records in this DBF file. This number of records must match the number of objects in the pending SHP file.
Get Field Info: this function gets field info at a given field index. It returns the field name, the field width, the number of decimals in this field and the field type (as return value). The field type can be integer, double, string or logical.
Is Attribute Null: checks if an attribute is null for a given shape index (object index) and field name.
Read Double Attribute: reads a double attribute for a given shape index (object index) and field name.
Read String Attribute: reads a string attribute for a given shape index (object index) and field name.
Read Logical Attribute: reads a logical attribute for a given shape index (object index) and field name.
Read Int Attribute: reads an int attribute for a given shape index (object index) and field name.