Revision 47264 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.geometry/org.gvsig.fmap.geometry.api/src/main/java/org/gvsig/fmap/geom/GeometryUtils.java

View differences:

GeometryUtils.java
24 24
package org.gvsig.fmap.geom;
25 25

  
26 26
import java.awt.geom.Point2D;
27
import java.util.ArrayList;
28
import java.util.Collections;
27 29
import java.util.List;
28 30
import java.util.Locale;
29 31
import java.util.Objects;
30 32
import java.util.regex.Matcher;
31 33
import java.util.regex.Pattern;
34
import javax.json.JsonArray;
35
import javax.json.JsonObject;
36
import javax.json.JsonValue;
32 37
import org.apache.commons.lang3.StringUtils;
33 38
import org.cresques.cts.IProjection;
34 39
import org.gvsig.euclidean.EuclideanLine2D;
......
36 41
import org.gvsig.fmap.geom.aggregate.MultiLine;
37 42
import org.gvsig.fmap.geom.aggregate.MultiPoint;
38 43
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
44
import org.gvsig.fmap.geom.aggregate.MultiPrimitive;
39 45
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
40 46
import org.gvsig.fmap.geom.exception.CreateGeometryException;
41 47
import org.gvsig.fmap.geom.operation.GeometryOperationException;
......
49 55
import org.gvsig.fmap.geom.primitive.Polygon;
50 56
import org.gvsig.fmap.geom.primitive.Spline;
51 57
import org.gvsig.fmap.geom.type.GeometryType;
58
import org.gvsig.json.Json;
52 59
import org.gvsig.tools.exception.BaseException;
53 60
import org.gvsig.tools.util.ToolsUtilLocator;
54 61

  
......
1130 1137
//        System.out.println(g);
1131 1138
//    }
1132 1139

  
1140
    public static final List<Geometry> extractFrom(String s, IProjection proj) {
1141
        try {
1142
            if( StringUtils.isBlank(s) ) {
1143
                return null;
1144
            }
1145
            Geometry geom = createFrom(s,proj);
1146
            if( geom!=null ) {
1147
                if( geom.getProjection()==null ) {
1148
                    geom.setProjection(proj);
1149
                }
1150
                return Collections.singletonList(geom);
1151
            }        
1152
            JsonValue json = Json.createObject(s);
1153
            if( json == null ) {
1154
                json = Json.createArray(s);
1155
                if( json == null ) {
1156
                    return null;
1157
                }
1158
            }
1159
            return extractFrom(json, proj);
1160
        } catch(Exception e) {
1161
            return null;
1162
        }
1163
    }        
1164
        
1165
    public static final List<Geometry> extractFrom(JsonValue json, IProjection proj) {
1166
        ArrayList<Geometry> geoms = new ArrayList<>();
1167
        if( extractFrom(json, proj, geoms) && !geoms.isEmpty() ) {
1168
            return geoms;
1169
        }
1170
        return null;
1171
    }
1133 1172
    
1173
    private static boolean extractFrom(JsonValue json, IProjection proj, List<Geometry> geoms) {
1174
        try {
1175
            if( json == null ) {
1176
                return false;
1177
            }
1178
            if( json instanceof JsonObject ) {
1179
                boolean x = false; 
1180
                for (JsonValue value : ((JsonObject)json).values()) {
1181
                    if( extractFrom(value, proj, geoms) ) {
1182
                        x = true;
1183
                    }
1184
                }
1185
                return x;
1186
            }
1187

  
1188
            if( json instanceof JsonArray ) {
1189
                boolean x = false; 
1190
                for (JsonValue value : ((JsonArray)json)) {
1191
                    if( extractFrom(value, proj, geoms) ) {
1192
                        x = true;
1193
                    }
1194
                }
1195
                return x;
1196
            }
1197
            Object x = Json.toObject(json);        
1198
            if( x == null ) {
1199
                return false;
1200
            }
1201
            if( x instanceof Geometry ) {
1202
                Geometry geom = (Geometry) x;
1203
                if( geom.getProjection()==null ) {
1204
                    geom.setProjection(proj);
1205
                }
1206
                geoms.add(geom);
1207
                return true;
1208
            }
1209

  
1210
            String s = Objects.toString(x, null);
1211
            if( StringUtils.isBlank(s) ) {
1212
                return false;
1213
            }
1214
            Geometry geom = createFrom(s, proj);
1215
            if( geom!=null ) {
1216
                if( geom.getProjection()==null ) {
1217
                    geom.setProjection(proj);
1218
                }
1219
                geoms.add(geom);
1220
                return true;
1221
            }
1222
            return false;
1223
        } catch(Exception e) {
1224
            return false;
1225
        }
1226
    }
1134 1227
    
1228
    public static final Geometry toAggregate(List<Geometry> geoms) {
1229
        if( geoms==null || geoms.isEmpty() ) {
1230
            return null;
1231
        }
1232
        try {
1233
            GeometryManager manager = GeometryLocator.getGeometryManager();
1234
            Geometry first = geoms.get(0);
1235
            int firsttype = first.getType();
1236
            int type = firsttype;
1237
            if( manager.isSubtype(Geometry.TYPES.SURFACE, type) ) {
1238
                type = Geometry.TYPES.MULTIPOLYGON;
1239
            } else if( manager.isSubtype(Geometry.TYPES.CURVE, type) ) {
1240
                type = Geometry.TYPES.MULTILINE;
1241
            } else if( manager.isSubtype(Geometry.TYPES.POINT, type) ) {
1242
                type = Geometry.TYPES.MULTIPOINT;
1243
            } else if( manager.isSubtype(Geometry.TYPES.MULTISURFACE, type) ) {
1244
                type = Geometry.TYPES.MULTIPOLYGON;
1245
            } else if( manager.isSubtype(Geometry.TYPES.MULTICURVE, type) ) {
1246
                type = Geometry.TYPES.MULTILINE;
1247
            } else if( manager.isSubtype(Geometry.TYPES.MULTIPOINT, type) ) {
1248
                type = Geometry.TYPES.MULTIPOINT;
1249
            } else {
1250
                return null;
1251
            }
1252
            int firstsubtype = first.getGeometryType().getSubType();
1253
            MultiPrimitive multi = (MultiPrimitive) manager.create(type, firstsubtype);
1254
            for (Geometry geom : geoms) {
1255
                if( firsttype != type ) {
1256
                    return null; // No soportado. El agregado debe ser homogeneo.
1257
                }
1258
                multi.addPrimitives(geom);
1259
            }
1260
            return multi;
1261
        } catch(Exception e) {
1262
            return null;
1263
        }
1264

  
1265
    }
1266

  
1135 1267
}

Also available in: Unified diff