Se puede resolver con dos archivos VRT encadenados y un poco de OGR SQL. El primer VRT
(por ejemplo remapped_csv.vrt
) es:
<OGRVRTDataSource>
<OGRVRTLayer name="remapped_csv">
<SrcDataSource>test.csv</SrcDataSource>
<SrcSQL>SELECT *, SUBSTR(latlon,2,5) AS lat, SUBSTR(latlon,9,12) AS lon FROM test</SrcSQL>
</OGRVRTLayer>
</OGRVRTDataSource>
El segundo VRT
(por ejemplo result.vrt
) es:
<OGRVRTDataSource>
<OGRVRTLayer name="result">
<SrcDataSource>remapped_csv.vrt</SrcDataSource>
<GeometryType>wkbPoint</GeometryType>
<LayerSRS>WGS84</LayerSRS>
<GeometryField encoding="PointFromColumns" x="lon" y="lat"/>
<SrcSQL>SELECT name, date, value, lat, lon FROM remapped_csv</SrcSQL>
</OGRVRTLayer>
</OGRVRTDataSource>
Este es el resultado final:
>ogrinfo result.vrt result
INFO: Open of `result.vrt'
using driver `VRT' successful.
Layer name: result
Geometry: Point
Feature Count: 5
Extent: (40.200000, 100.200000) - (41.200000, 102.000000)
Layer SRS WKT:
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
TOWGS84[0,0,0,0,0,0,0],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9108"]],
AUTHORITY["EPSG","4326"]]
name: String (0.0)
date: String (0.0)
value: String (0.0)
lat: String (0.0)
lon: String (0.0)
OGRFeature(result):1
name (String) = a
date (String) = 20130101
value (String) = 0
lat (String) = 100.8
lon (String) = 40.2
POINT (40.2 100.8)
OGRFeature(result):2
name (String) = b
date (String) = 20130101
value (String) = 1
lat (String) = 101.2
lon (String) = 40.4
POINT (40.4 101.2)
OGRFeature(result):3
name (String) = c
date (String) = 20130102
value (String) = 1
lat (String) = 100.2
lon (String) = 41.2
POINT (41.2 100.2)
OGRFeature(result):4
name (String) = d
date (String) = 20130103
value (String) = 2
lat (String) = 102.0
lon (String) = 40.7
POINT (40.7 102.0)
OGRFeature(result):5
name (String) = e
date (String) = 20130104
value (String) = 3
lat (String) = 100.6
lon (String) = 41.0
POINT (41.0 100.6)
Versión mejorada de remapped_csv.vrt
utilizando el dialecto SQLite (SQLite >= 3.7.15, porque el instr
fue añadida a las funciones del núcleo de SQLite desde esta versión):
<OGRVRTDataSource>
<OGRVRTLayer name="remapped_csv">
<SrcDataSource>test2.csv</SrcDataSource>
<SrcSQL dialect="sqlite">SELECT *, SUBSTR(latlon,2,INSTR(latlon,"\,")-1) AS lat, SUBSTR(latlon,INSTR(latlon,"\,")+2,-1) AS lon FROM test2</SrcSQL>
</OGRVRTLayer>
</OGRVRTDataSource>
No lo he probado (porque tengo GDAL construido con SQLite 3.7.9 en este momento), pero debería funcionar.
Esta es una versión de trabajo de remapped_csv.vrt
utilizando el enfoque de recorte:
<OGRVRTDataSource>
<OGRVRTLayer name="remapped_csv">
<SrcDataSource>test2.csv</SrcDataSource>
<SrcSQL dialect="sqlite">SELECT *, rtrim(rtrim(latlon,'"+-.0123456789'),'\,') AS lat, ltrim(ltrim(latlon,' "+-.0123456789'),'\,') AS lon FROM test2</SrcSQL>
</OGRVRTLayer>
</OGRVRTDataSource>