Aloha.
I’m trying to figure out how to extract attributed data from a shapefile so I can construct a cpt file (makecpt). I have successfully plotted a map from the shapefile using:
but cannot figure out how to extract the KAcreFt attribute into a file that I can use to generate a colormap. I’ve tried gmt convert but seem to end up with all of the vertices of all of the polygons with the attribute value as the 3-rd column. Any advice is welcome.
Aloha.
I see I had this problem before and I don’t think it was ever solved. This answer is not helpful as I’ve tried a wide-range of makecpt and psxy arguments.
The problem seems to be that makecpt is using one of the geometry fields to generate the cpt. The attribute arguments of psxy don’t seem to have any effect and there is a cryptic comment about changing the selection of input variables in makecpt to somehow use i ; whatever that means.
More experimentation shows that gmtinfo only sees the geometry of the shapefile and none of the attributes. Is there any method to read shapefiles that preserves attributes? psxy seems to be the only one.
gdal’s ogrinfo can extract all attribute values that exist in a shapefile. Assuming your attribute name is KAcreFt and shapefile name is shapefile_name.shp:
ogrinfo -sql "SELECT DISTINCT KAcreFt FROM shapefile_name" shapefile_name.shp
Mahalo. The ogrinfo solution works although the syntax is confusing (layers, filenames, …). It took a while to get it to work since SQL syntax error are amazingly terse.
I also found this approach to work well. That is, writing a CSV file and then extracting whatever field is wanted. In the example below, there is only one field but gawk could be used to pick from many.