1

I want to save datetime in shapefile, but the date attribute only save the date part. For example, I put "Thu Feb 28 01:20:00 EST 2019" in date attribute, but when I print it from the feature I built, I got "Thu Feb 28 00:00:00 EST 2019".
The user guide of shapefile is here, and it said that it supports "Date - TimeStamp interpretation that is both date and time".
By the way, I am working with the FIRMS data, and found that it save date and time in two fields, they do like this may because of this problem. If this is a bug, could geotools developers fix it, please?

RWBY
  • 11
  • 2

1 Answers1

0

Storing the time is technically outside the dbf3 specification that shapefiles are built on. Ideally you would switch to a better file format such as geopackage. But you can enable out of specification behaviour by setting the org.geotools.shapefile.datetime property (either in your program or as a system setting). I've opened a documentation bug as there doesn't seem to be much mention of this setting other than in an old mailing list post.

This code demonstrates how to use it:

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());

Without the property set it outputs:

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]

and with it you get:

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]
Ian Turton
  • 10,018
  • 1
  • 28
  • 47