Changeset 30982 in osm for applications/editors/josm


Ignore:
Timestamp:
2015-02-14T21:48:38+01:00 (10 years ago)
Author:
donvip
Message:

[josm_opendata] fix #josm11097 - correct handling of dates in shapefiles

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/geographic/ShpReader.java

    r30796 r30982  
    1717import java.nio.file.Files;
    1818import java.nio.file.Path;
     19import java.text.SimpleDateFormat;
     20import java.util.Date;
    1921import java.util.HashMap;
    2022import java.util.HashSet;
     
    5961    private final ShpHandler handler;
    6062    private final Set<OsmPrimitive> featurePrimitives = new HashSet<>();
    61    
     63
    6264    public ShpReader(ShpHandler handler) {
    6365        super(handler, NationalHandlers.DEFAULT_SHP_HANDLERS);
     
    7880        }
    7981    }
    80    
    81     private void parseFeature(Feature feature, final Component parent) 
     82
     83    private void parseFeature(Feature feature, final Component parent)
    8284            throws UserCancelException, GeoMathTransformException, FactoryException, GeoCrsException, MismatchedDimensionException, TransformException {
    8385        featurePrimitives.clear();
     
    8688
    8789            GeometryDescriptor desc = geometry.getDescriptor();
    88            
     90
    8991            if (crs == null) {
    9092                if (desc != null && desc.getCoordinateReferenceSystem() != null) {
     
    114116                }
    115117            }
    116            
     118
    117119            OsmPrimitive primitive = null;
    118            
     120
    119121            if (geometry.getValue() instanceof Point) {
    120122                primitive = createOrGetEmptyNode((Point) geometry.getValue());
    121                
     123
    122124            } else if (geometry.getValue() instanceof GeometryCollection) { // Deals with both MultiLineString and MultiPolygon
    123125                GeometryCollection mp = (GeometryCollection) geometry.getValue();
    124                 int nGeometries = mp.getNumGeometries(); 
     126                int nGeometries = mp.getNumGeometries();
    125127                if (nGeometries < 1) {
    126128                    Main.error("empty geometry collection found");
     
    128130                    Relation r = null;
    129131                    Way w = null;
    130                    
     132
    131133                    for (int i=0; i<nGeometries; i++) {
    132134                        Geometry g = mp.getGeometryN(i);
     
    166168                Main.debug("-------------------------------------------------------------");
    167169            }
    168            
     170
    169171            if (primitive != null) {
    170172                // Read primitive non geometric attributes
     
    178180        transform = null;
    179181        try {
    180             if (file != null) { 
     182            if (file != null) {
    181183                Map<String, Serializable> params = new HashMap<>();
    182184                Charset charset = null;
     
    205207                    throw new IOException(tr("Unable to find a data store for file {0}", file.getName()));
    206208                }
    207                
     209
    208210                String[] typeNames = dataStore.getTypeNames();
    209211                String typeName = typeNames[0];
    210    
     212
    211213                FeatureSource<?,?> featureSource = dataStore.getFeatureSource(typeName);
    212214                FeatureCollection<?,?> collection = featureSource.getFeatures();
    213215                FeatureIterator<?> iterator = collection.features();
    214                
     216
    215217                if (instance != null) {
    216218                    instance.beginTask(tr("Loading shapefile ({0} features)", collection.size()), collection.size());
    217219                }
    218                
     220
    219221                int n = 0;
    220                
     222
    221223                Component parent = instance != null ? instance.getWindowParent() : Main.parent;
    222                
     224
    223225                try {
    224226                    while (iterator.hasNext()) {
     
    258260        return ds;
    259261    }
    260    
     262
    261263    private static final void readNonGeometricAttributes(Feature feature, OsmPrimitive primitive) {
    262             try {
    263         for (Property prop : feature.getProperties()) {
    264             if (!(prop instanceof GeometryAttribute)) {
    265                 Name name = prop.getName();
    266                 Object value = prop.getValue();
    267                 if (name != null && value != null) {
    268                     String sName = name.toString();
    269                     String sValue = value.toString();
    270                     if (!sName.isEmpty() && !sValue.isEmpty()) {
    271                                                primitive.put(sName, sValue);
    272                     }
    273                 }
    274             }
    275         }
    276             } catch (Exception e) {
    277                 e.printStackTrace();
    278             }
     264        try {
     265            for (Property prop : feature.getProperties()) {
     266                if (!(prop instanceof GeometryAttribute)) {
     267                    Name name = prop.getName();
     268                    Object value = prop.getValue();
     269                    if (name != null && value != null) {
     270                        String sName = name.toString();
     271                        String sValue = value.toString();
     272                        if (value instanceof Date) {
     273                            sValue = new SimpleDateFormat("yyyy-MM-dd").format(value);
     274                        }
     275                        if (!sName.isEmpty() && !sValue.isEmpty()) {
     276                            primitive.put(sName, sValue);
     277                        }
     278                    }
     279                }
     280            }
     281        } catch (Exception e) {
     282            Main.error(e);
     283        }
    279284    }
    280285
     
    285290        return n;
    286291    }
    287    
     292
    288293    @Override
    289294    protected <T extends OsmPrimitive> T addOsmPrimitive(T p) {
Note: See TracChangeset for help on using the changeset viewer.