2

I'm using an OGR_Style field to set the styles for a MapInfo Tab file with Geopanadas. Does anyone know if there's a way use the OGR_Style field for styling but not include it as a field in the file?

2
  • Could you provide a small dataset with OGR_STYLE for testing?
    – user30184
    Commented May 23 at 20:55
  • Hi, thanks :) you should be able to reproduce with this: import geopandas as gpd from shapely.geometry import Point d = {'field1': ['name1', 'name2'], 'geometry': [Point(1, 2), Point(2, 1)]} gdf = gpd.GeoDataFrame(d, crs="EPSG:4326") gdf['OGR_STYLE'] = 'SYMBOL(c:#00FF00,s:12pt,id:"font-sym-75,ogr-sym-9",f:"MapInfo_Cartographic")' gdf.to_file("your_path/sample.tab", driver='MapInfo File')
    – Prosper
    Commented May 24 at 8:37

1 Answer 1

0

It is possible to drop OGR_STYLES as an attribute by using the GDAL ExecuteSQL() method https://gdal.org/doxygen/classGDALDataset.html#a5b65948b1e15fa63e96c0640eb6c5d7c but I do not know if it is possible to utilize it from geopandas.

Notice that the following works with MapInfo format because it has a native support for styles. Once the OGR_STYLE definition is turned into native MapInfo styles the original style strings can be deleted.

Proof of concept by using a GeoJSON file as source data:

{
"type": "FeatureCollection",
"name": "styletest",
"features": [
{ "type": "Feature", "properties": { "foo": "bar", "OGR_STYLE": "BRUSH(fc:#0000BD)" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -1.0, 0.0 ], [ -1.0, 1.0 ], [ 0.0, 1.0 ], [ 0.0, 0.0 ], [ -1.0, 0.0 ] ] ] } }
]
}

As you know, ogrinfo reports now OGR_STYLE both as an attribute and as a Style, and both of them are translated into formats that support styles, like MapInfo.

ogrinfo styletest.json -al
...
OGRFeature(styletest):0
  foo (String) = bar
  OGR_STYLE (String) = BRUSH(fc:#0000BD)
  Style = BRUSH(fc:#0000BD)
  POLYGON ((-1 0,-1 1,0 1,0 0,-1 0))

However, because OGR_STYLE is not only something that gets transformed into an internally used Style but also a normal attribute, it is possible to use SQL and leave it out from the selection. Than can be done by selecting one by one the attributes to include SELECT attr1, attr2, attr2....FROM... but for excluding just "OGR_STYLE" from the attributes the brand new feature in the OGR SQL dialect "EXCLUDE" https://gdal.org/user/ogr_sql_dialect.html#list-operators is handy. EXCLUDE support is new to GDAL version 3.9.0

The special character * is taken to mean "all fields". The syntax * EXCLUDE ([fields]) can be used to select all fields except those listed in parentheses.

Convert the data into MapInfo without OGR_STYLE attbibute:

ogr2ogr -f "MapInfo File" styletest.tab styletest.json -sql "select * EXCLUDE(OGR_STYLE) from styletest"

Ogrinfo from styletest.tab shows:

OGRFeature(styletest):1
  foo (String) = bar
  Style = BRUSH(fc:#0000bd,id:"mapinfo-brush-2,ogr-brush-0");PEN(w:1px,c:#000000,id:"mapinfo-pen-2,ogr-pen-0",cap:r,j:r)
  POLYGON ((-1 0,-1 1,0 1,0 0,-1 0))

Without excluding OGR_STYLES with -sql the result would have been

OGRFeature(styletest2):1
  foo (String) = bar
  OGR_STYLE (String) = BRUSH(fc:#0000BD)
  Style = BRUSH(fc:#0000bd,id:"mapinfo-brush-2,ogr-brush-0");PEN(w:1px,c:#000000,id:"mapinfo-pen-2,ogr-pen-0",cap:r,j:r)
  POLYGON ((-1 0,-1 1,0 1,0 0,-1 0))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.