我想在shapefile中保存日期时间,但日期属性只保存日期部分。例如,我把日期属性中的“Thu Feb 28 01:20:00 EST 2019”,但是当我从我建造的特征中打印出来时,我得到了“Thu Feb 28 00:00:00 EST 2019”。 shapefile的用户指南是here,它说它支持“Date - TimeStamp解释,既是日期又是时间”。 顺便说一句,我正在使用FIRMS data,并发现它在两个字段中保存日期和时间,他们这样做可能是因为这个问题。如果这是一个错误,geotools开发人员可以修复它,好吗?
存储时间在技术上不在dbf3规范之外,因为shapefile是建立在其上的。理想情况下,您将切换到更好的文件格式,如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]