CSV to Shapefile - A WaterObjects.NET Tool

CSV to Shapefile - A WaterObjects.NET Tool
NOTE:  
Introduction

This tool takes a Comma Separated Values (CSV) as an input and based on the information selected, it creates a shapefile. This tool particularly comes in handy when one needs to run LoadBuilder and does not have any means to convert a spreadsheet type of data to a shapefile (as LoadBuilder only supports shapefile format).

The programming details covered herein are applicable to several of Bentley’s hydraulics and hydrology (H&H) applications including WaterGEMS, WaterCAD, HAMMER, SewerGEMS, SewerCAD, SewerGEMS Sanitary, CivilStorm, StormCAD, PondPack etc. In fact, the exact same lines of code can be used to create the tool that can work with most H&H applications. The tool is currently built against WaterGEMS SS3.

How to Run the Tool

If you simply obtain the executable (.exe) file then make sure to place this file in the right directory. Since the tool is built for WaterGEMS, the .exe needs to be placed in the %ProgramFiles%\Bentley\WaterGEMS\ directory. Once the exe file is in the right location, run it and you will see the window as shown in the figure above.

In the CSVtoSHP window, click on the “Browse” button and select the CSV file. This will cause the window to update the User Interface and include a small box which represents a column in the CSV file. If a column that exists in the source but don’t want to have that in a converted shapefile, un-check the box. An appropriate data format must be provided. In doubt, select ‘string_text’ option. If a column name is longer than 10 characters or like to give a different name, provided box can be filled in. Finally, X-Coordinate and Y-Coordinate must also be provided as shapefile geometry will be based on these coordinates.

Once the necessary options are selected/entered, click on the apply button (little check mark button in middle right). This will validate the data and will load the data from CSV file to a table. If there are any issues, a validation window will list the issues. If all the input options are correct, a preview of data table will be displayed on clicking the apply button.

Finally, click on the Export button (bottom right button) and it will create a shapefile at the given location. To, change the given location, click on the button left of Export button and provide a desired location.

Here is a little video to show the steps:

Programming Details

Bentleys WaterObjects.NET API will be primarily discussed in the sections below using C# as a programming language. It is assumed that the reader has some basic knowledge about the .NET programming.

References used

Haestad.Shapefile
Haestad.Support.Support

Namespaces used

Haestad.Shapefile
Haestad.Support.Support

How to create a shapefile

There are three basic methods involved to create a shapefile using WaterObjects.NET API, creating a data file, writing actual data and finally writing geometry.

To create data file, we need to use CreateDataFile method from a new instance of ShapefileDataSourceWriter. This CreateDataFile method takes four arguments, file name, fields to create (array of DBFFieldInfo), overwrite and the shapefile type. Array of DBFFieldInfo is a list of DBFFieldInfo and each DBFFieldInfo basically consist of, field name, field data type and field length info. Once a data file is created, loop through the rows of the data source call the WriteFieldData method on ShapefileDataSourceWriter to write the values. To write the geometry, call WriterPointGeometry method.

ShapefileDataSourceWriter writer = new ShapefileDataSourceWriter();
if (writer.CreateDataFile(textBoxShapeFilePath.Text, fieldsToCreate.ToArray(), true, ShapeType.Point))
{
   foreach (DataRow row in csvTable.Rows)
   {
      writer.AddRecordBegin();

      foreach (DBFFieldInfo field in fieldsToCreate)
      {
         if (!WriteValueToShapefile(writer, field.FieldName, row[field.FieldName]))
            createdSuccessfully = false;
      }

      if (!WriteGeometryField(writer, new GeometryPoint(Convert.ToDouble(row[comboBoxXCoord.Text]),
                    Convert.ToDouble(row[comboBoxYCoord.Text]))))
         createdSuccessfully = false;

       writer.AddRecordEnd();
   }
}
private bool WriteGeometryField(ShapefileDataSourceWriter writer, GeometryPoint geometryPoint)
{
   return writer.WritePointGeometry(geometryPoint);
}
private bool WriteValueToShapefile(ShapefileDataSourceWriter writer, string fieldName, object value)
{
   return writer.WriteFieldData(value, fieldName);
}
Notes

In the actual solution (Visual Studio file), there are a lot more lines of code than the code discussed above, and may not have an exact match. There, the solution file may exhibit slightly different code lines than above. The above code is to highlight the key codes for the task.

What if the tool does not work or help is needed?

-If you just receive the exe file, make sure the file is placed in the right location