3

I have a bunch of input values in this format:

2014-05-14 17:42:18

And I would like to store them in a field in QGIS. I am using the Python API for GDAL/OGR. I notice that QGIS supports a field type "QDate", but it seems that it can only handle dates with no time, as such:

2014-05-14

I'm just wondering if there is a way to get around this or am I stuck storing the timestamp as a string?

4

1 回答 1

3

我猜 python OGR API 有一种方法来定义一个日期时间字段并在其中存储数据(它在OFRFieldTypeenum中定义。你必须使用 OGR 定义的OFTDateTime(或OFTDateOFTTime)类型。

因此,您可以执行以下操作:

date_field = ogr.FieldDefn("date", ogr.OFTDateTime)
your_layer.CreateField(date_field)

然后您可以通过将日期作为字符串传递来设置功能的值:

feature = ogr.Feature(your_layer.GetLayerDefn())
feature.SetField("date", "2014-05-14 17:42:18")

结果可能取决于您的输出数据结构(如果它定义/支持日期时间类型)以及您用来显示它们的桌面 GIS 软件。

编辑:我测试过这样写一个shapefile,然后在QGIS中打开它;日期字段被正确识别为字段QDate,正如你所说的时间没有显示所以我查看了.dbf 文件并且时间部分似乎没有被写入。

于 2016-01-13T19:38:03.863 回答