shapefile不在日期属性中保存时间部分

时间:2019-02-28 06:47:49

标签: geotools

我想将datetime保存在shapefile中,但是date属性仅保存日期部分。例如,我在日期属性中添加了“ Thu Feb 28 01:20:00 EST 2019”,但是当我从构建的功能中打印它时,我得到了“ Thu Feb 28 00: 00:00 EST 2019”。
shapefile的用户指南为here,它说它支持“日期和时间的日期和时间时间戳解释”。
顺便说一句,我正在使用FIRMS data,发现它在两个字段中保存了日期和时间,因为这个问题,他们这样做确实是可行的。如果这是一个错误,geotools开发人员可以修复它吗?

1 个答案:

答案 0 :(得分:0)

技术上来说,存储时间在构建shapefile的dbf3规范之外。理想情况下,您将切换为更好的文件格式,例如geopackage。但是您可以通过设置org.geotools.shapefile.datetime属性(在程序中或作为系统设置)来启用不符合规范的行为。我已经打开了一个documentation bug,因为除了在old mailing list post中似乎没有太多提及此设置之外。

此代码演示了如何使用它:

File out = new File("date.shp");
out.createNewFile();
FileDataStore ds = FileDataStoreFinder.getDataStore(out);
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName("test");
typeBuilder.add("the_geom", Point.class);
typeBuilder.setCRS(DefaultGeographicCRS.WGS84);
typeBuilder.setDefaultGeometry("the_geom");
typeBuilder.add("date", Date.class);
typeBuilder.add("timestamp", Calendar.class);
SimpleFeatureType schema = typeBuilder.buildFeatureType();

ds.createSchema(schema);

SimpleFeatureStore featureStore = (SimpleFeatureStore) ds.getFeatureSource();
System.setProperty("org.geotools.shapefile.datetime", "true");
GeometryFactory gf = new GeometryFactory();
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(schema);
builder.set("date", new Date());
builder.set("timestamp", Calendar.getInstance());
builder.set("the_geom", gf.createPoint(new Coordinate(0, 0)));
SimpleFeature f = builder.buildFeature(null);
System.out.println(f);

featureStore.addFeatures(DataUtilities.collection(f));
ds.dispose();

FileDataStore in = FileDataStoreFinder.getDataStore(out);

  System.out.println(in.getFeatureReader().next());

没有设置属性,它会输出:

SimpleFeatureImpl:test=[SimpleFeatureImpl.Attribute: the_geom<the_geom id=fid--4318b68d_1693e3b2f4e_-8000>=POINT (0 0), SimpleFeatureImpl.Attribute: date<date id=fid--4318b68d_1693e3b2f4e_-8000>=Sat Mar 02 11:47:39 GMT 2019, SimpleFeatureImpl.Attribute: timestamp<timestamp id=fid--4318b68d_1693e3b2f4e_-8000>=java.util.GregorianCalendar[time=1551527259980,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2019,MONTH=2,WEEK_OF_YEAR=9,WEEK_OF_MONTH=0,DAY_OF_MONTH=2,DAY_OF_YEAR=61,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=47,SECOND=39,MILLISECOND=980,ZONE_OFFSET=0,DST_OFFSET=0]]
SimpleFeatureImpl:date=[SimpleFeatureImpl.Attribute: the_geom<Point id=date.1>=POINT (0 0), SimpleFeatureImpl.Attribute: date<date id=date.1>=Sat Mar 02 00:00:00 GMT 2019, SimpleFeatureImpl.Attribute: timestamp<timestamp id=date.1>=Sat Mar 02 00:00:00 GMT 2019]

并获得:

SimpleFeatureImpl:test=[SimpleFeatureImpl.Attribute: the_geom<the_geom id=fid--6d9c8770_1693e3c0660_-8000>=POINT (0 0), SimpleFeatureImpl.Attribute: date<date id=fid--6d9c8770_1693e3c0660_-8000>=Sat Mar 02 11:48:35 GMT 2019, SimpleFeatureImpl.Attribute: timestamp<timestamp id=fid--6d9c8770_1693e3c0660_-8000>=java.util.GregorianCalendar[time=1551527315038,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2019,MONTH=2,WEEK_OF_YEAR=9,WEEK_OF_MONTH=0,DAY_OF_MONTH=2,DAY_OF_YEAR=61,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=48,SECOND=35,MILLISECOND=38,ZONE_OFFSET=0,DST_OFFSET=0]]
SimpleFeatureImpl:date=[SimpleFeatureImpl.Attribute: the_geom<Point id=date.1>=POINT (0 0), SimpleFeatureImpl.Attribute: date<date id=date.1>=2019-03-02 11:48:35.038, SimpleFeatureImpl.Attribute: timestamp<timestamp id=date.1>=2019-03-02 11:48:35.038]