Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / symboltables / ProjectSymbolTable.java @ 44644

History | View | Annotate | Download (28.5 KB)

1
package org.gvsig.app.project.symboltables;
2

    
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Properties;
10
import org.apache.commons.io.IOUtils;
11
import org.apache.commons.lang3.Range;
12
import org.apache.commons.lang3.StringUtils;
13
import org.cresques.cts.IProjection;
14
import org.gvsig.app.ApplicationLocator;
15
import org.gvsig.app.ApplicationManager;
16
import org.gvsig.app.project.Project;
17
import org.gvsig.app.project.ProjectManager;
18
import org.gvsig.app.project.documents.Document;
19
import org.gvsig.app.project.documents.view.ViewDocument;
20
import org.gvsig.app.project.documents.view.ViewManager;
21
import org.gvsig.expressionevaluator.Expression;
22
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
23
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
24
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
25
import org.gvsig.expressionevaluator.Interpreter;
26
import org.gvsig.expressionevaluator.spi.AbstractFunction;
27
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
28
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
29
import org.gvsig.fmap.crs.CRSFactory;
30
import org.gvsig.fmap.dal.DALLocator;
31
import org.gvsig.fmap.dal.DataManager;
32
import org.gvsig.fmap.dal.HasDataStore;
33
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
34
import org.gvsig.fmap.dal.feature.Feature;
35
import org.gvsig.fmap.dal.feature.FeatureQuery;
36
import org.gvsig.fmap.dal.feature.FeatureSet;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.fmap.mapcontext.MapContext;
41
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
43
import org.gvsig.fmap.mapcontrol.AreaAndPerimeterCalculator;
44
import org.gvsig.temporarystorage.TemporaryStorageGroup;
45
import org.gvsig.temporarystorage.TemporaryStorageLocator;
46
import org.gvsig.temporarystorage.TemporaryStorageManager;
47
import org.gvsig.tools.util.UnmodifiableBasicList;
48

    
49
/**
50
 *
51
 * @author jjdelcerro
52
 */
53
@SuppressWarnings("UseSpecificCatch")
54
public class ProjectSymbolTable extends AbstractSymbolTable {
55
    public static final String PROJECT_GROUP = "Project";        
56

    
57
    public static final String AREA_NAME = "AREA";        
58
    public static final String PERIMETER_NAME = "PERIMETER";
59
    public static final String STORE_NAME = "STORE";
60
    public static final String FETCH_FIRST_NAME = "FETCH_FIRST";
61
    public static final String FETCH_FIRST_SELECTED_NAME = "FETCH_FIRST_SELECTED";
62
    public static final String FETCH_NAME = "FETCH";
63
    public static final String APPLICATION_NAME = "APPLICATION";
64
        
65
    static final String NAME = "Project";
66
    
67
    private static final String TABLE_DOCUMENT_TYPENAME = "project.document.table";
68
    
69
    private abstract class CachedValue<T> {
70

    
71
        T value = null;
72
        long lastAccess = 0;
73

    
74
        protected abstract void reload();
75

    
76
        public boolean isExpired() {
77
            long now = System.currentTimeMillis();
78
            return now - lastAccess > 3000;
79
        }
80

    
81
        public T get() {
82
            if (isExpired()) {
83
                reload();
84
            }
85
            lastAccess = System.currentTimeMillis();
86
            return value;
87
        }
88
    }
89

    
90
    private class ProjectValue extends CachedValue<Project> {
91

    
92
        @Override
93
        protected void reload() {
94
            value = ProjectManager.getInstance().getCurrentProject();
95
        }
96

    
97
    }
98

    
99
    private class CurrentViewValue extends CachedValue<ViewDocument> {
100

    
101
        @Override
102
        protected void reload() {
103
            ApplicationManager application = ApplicationLocator.getManager();
104
            ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
105
            value = viewdoc;
106
        }
107

    
108
    }
109

    
110
    private class CurrentViewEnvelopeValue extends CachedValue<Geometry> {
111

    
112
        @Override
113
        protected void reload() {
114
            ApplicationManager application = ApplicationLocator.getManager();
115
            ViewDocument viewdoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME);
116
            if( viewdoc == null ) {
117
                value = null;
118
                return;
119
            }
120
            value = viewdoc.getMapContext().getViewPort().getEnvelope().getGeometry();
121
        }
122

    
123
    }
124

    
125
    private class PropertiesValue extends CachedValue<Map<File, Properties>> {
126

    
127
        @Override
128
        protected void reload() {
129
            value = new HashMap<>();
130
        }
131
    }
132

    
133
    private class CurrentProjectFunction extends AbstractFunction {
134

    
135
        public CurrentProjectFunction() {
136
            super(
137
                    PROJECT_GROUP,
138
                    "project",
139
                    Range.is(0),
140
                    "Access to the current project loaded in gvSIG desktop.\n",
141
                    "project()",
142
                    null,
143
                    "Project"
144
            );
145
        }
146

    
147
        @Override
148
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
149
            return currentProject.get();
150
        }
151

    
152
    }
153

    
154
    private class ApplicationFunction extends AbstractFunction {
155

    
156
        public ApplicationFunction() {
157
            super(
158
                    PROJECT_GROUP,
159
                    APPLICATION_NAME,
160
                    Range.is(0),
161
                    "Access to the Application object.\n",
162
                    APPLICATION_NAME+"()",
163
                    null,
164
                    "ApplicationManager"
165
            );
166
        }
167

    
168
        @Override
169
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
170
            return ApplicationLocator.getApplicationManager();
171
        }
172

    
173
    }
174

    
175
    private class StoreFunction extends AbstractFunction {
176

    
177
        public StoreFunction() {
178
            super(
179
                    PROJECT_GROUP,
180
                    STORE_NAME,
181
                    Range.is(2), // Range.between(1, 2),
182
                    "Return the data store associated with a layer.", // or a table.\n" +
183
//                       "If receive a single parameter, it will correspond to " + 
184
//                       "the name of a table-type document, and it will return " + 
185
//                       "your data store. If you receive two parameters, you expect " +
186
//                       "to be the name of a view and a layer of it, returning the " +
187
//                       "data store of the layer.",
188
                    STORE_NAME+"({{view}}, layer)",
189
                    new String[]{
190
                        "view - String value with the name of a view, if view is null use current View.",
191
                        "layer - String value with the name of a layer in the indicated view"
192
//                        "table - String value with the name of document table"
193
                    },
194
                    "DataStore"
195
            );
196
        }
197

    
198
        @Override
199
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
200
            Project project = currentProject.get();
201
            switch (args.length) {
202
                case 1:
203
                    String tableName = getStr(args, 0);
204
                    Document document = project.getDocument(tableName, TABLE_DOCUMENT_TYPENAME);
205
                    if (document == null) {
206
                        throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't locate table document '"+tableName+"'.");
207
//                        return null;
208
                    }
209
                    if( document instanceof HasDataStore ) {
210
                        return ((HasDataStore) document).getDataStore();
211
                    }
212
                    throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't get store from table document '"+tableName+"'.");
213
//                    return null;
214

    
215
                case 2:
216
                    ViewDocument view;
217
                    String viewName = getStr(args, 0);
218
                    if( StringUtils.isBlank(viewName) ) {
219
                        view = (ViewDocument) project.getActiveDocument(ViewManager.TYPENAME);
220
                        if (view == null) {
221
                            throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't locate active View.");
222
//                            return null;
223
                        }
224
                    } else {
225
                        view = (ViewDocument) project.getDocument(viewName, ViewManager.TYPENAME);
226
                        if (view == null) {
227
                            throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't locate View '"+viewName+"'.");
228
//                            return null;
229
                        }
230
                    }
231
                    String layerName = getStr(args, 1);
232
                    FLayer layer = view.getLayer(layerName);
233
                    if( layer == null ) {
234
                        throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't locate layer '"+layerName+"' in View '"+viewName+"'.");
235
                    }
236
                    if( layer instanceof HasDataStore ) {
237
                        return ((HasDataStore)layer).getDataStore();
238
                    }
239
                    throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't get the store from layer '"+layerName+"' in View '"+viewName+"'.");
240
//                    return null;
241
            }
242
            return null;
243
        }
244
        
245
    }
246
    
247
    private class FetchFunction extends AbstractFunction {
248

    
249
        public FetchFunction() {
250
            super(
251
                    PROJECT_GROUP,
252
                    FETCH_NAME,
253
                    Range.between(2, 4),
254
                    "Access to the features of a table and retuen a list with the values.",
255
                    FETCH_NAME+"({{value}}, store, where, order)",
256
                    new String[]{
257
                        "value - value to retrieve from the store, usually an expression in the columns of this are involved.",
258
                        "store - data store from which values will be collected",
259
                        "where - Optional. String value with a filter expression",
260
                        "order - Optional. String value with the order. must be a string with the names of separate fields with commas"
261
                    },
262
                    "Object"
263
            );
264
        }
265

    
266
        @Override
267
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
268
            Object value;
269
            String where = null;
270
            String order = null;
271
            String expression_s = getStr(args, 0);
272
            FeatureStore store = (FeatureStore) getObject(args, 1);
273
            switch (args.length) {
274
                case 3:
275
                    where = getStr(args, 2);
276
                    break;
277
                case 4:
278
                    where = getStr(args, 2);
279
                    order = getStr(args, 3);
280
                    break;
281
            }
282
            try {
283
                List<Feature> features;
284
                if (where == null && order == null) {
285
                    features = store.getFeatures();
286
                } else {
287
                    FeatureQuery query = store.createFeatureQuery();
288
                    if (where != null) {
289
                        query.addFilter(where);
290
                    }
291
                    if (order != null) {
292
                        query.getOrder().add(order);
293
                    }
294
                    query.retrievesAllAttributes();
295
                    features = store.getFeatures(query);
296
                }
297
                UnmodifiableBasicList<Object> list = new ListOfFeaturesWrapper(
298
                        features,
299
                        interpreter.getSymbolTable(), 
300
                        expression_s
301
                );
302
                return list;
303
            } catch (Exception ex) {
304
                throw new ExpressionRuntimeException("Problems calling '"+FETCH_NAME+"' function", ex);
305
            }
306
        }
307
    }
308

    
309
    private class FetchFirstFunction extends AbstractFunction {
310

    
311
        public FetchFirstFunction() {
312
            super(
313
                    PROJECT_GROUP,
314
                    FETCH_FIRST_NAME,
315
                    Range.between(2, 4),
316
                    "Access to the first feature of the layer, and "
317
                    + "return the value of the attribute.",
318
                    FETCH_FIRST_NAME+"({{value}}, store, where, order)",
319
                    new String[]{
320
                        "value - value to retrieve from the store, usually an expression in the columns of this are involved.",
321
                        "store - data store from which values will be collected",
322
                        "where - Optional. String value with a filter expression",
323
                        "order - Optional. String value with the order. must be a string with the names of separate fields with commas"
324
                    },
325
                    "Object"
326
            );
327
        }
328

    
329
        @Override
330
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
331
            Object value;
332
            String where = null;
333
            String order = null;
334
            String expression_s = getStr(args, 0);
335
            FeatureStore store = (FeatureStore) getObject(args, 1);
336
            if( store == null ) {
337
                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_NAME+"' function, the store is null.");
338
            }
339
            switch (args.length) {
340
                case 3:
341
                    where = getStr(args, 2);
342
                    break;
343
                case 4:
344
                    where = getStr(args, 2);
345
                    order = getStr(args, 3);
346
                    break;
347
            }
348
            try {
349
                FeatureSet set;
350
                if (where == null && order == null) {
351
                    set = store.getFeatureSet();
352
                } else {
353
                    FeatureQuery query = store.createFeatureQuery();
354
                    if (where != null) {
355
                        query.addFilter(where);
356
                    }
357
                    if (order != null) {
358
                        query.getOrder().add(order);
359
                    }
360
                    query.retrievesAllAttributes();
361
                    set = store.getFeatureSet(query);
362
                }
363
                Feature feature = set.first();
364
                if (feature == null) {
365
                    return null;
366
                }
367
                DataManager dataManager = DALLocator.getDataManager();
368
                FeatureSymbolTable symbolTable = dataManager.createFeatureSymbolTable();
369
                symbolTable.setFeature(feature);
370

    
371
                ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
372
                Expression expression = expManager.createExpression();
373
                expression.setPhrase(expression_s);
374
                value = expression.execute(symbolTable.createParent());
375
                
376
                return value;
377
            } catch (Exception ex) {
378
                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_NAME+"' function", ex);
379
            }
380
        }
381
    }
382

    
383
    private class FetchFirstSelectedFunction extends AbstractFunction {
384

    
385
        public FetchFirstSelectedFunction() {
386
            super(
387
                    PROJECT_GROUP,
388
                    FETCH_FIRST_SELECTED_NAME,
389
                    Range.is(2),
390
                    "Access to the first feature of the selection.",
391
                    FETCH_FIRST_SELECTED_NAME+"({{value}}, store)",
392
                    new String[]{
393
                        "value - value to retrieve from the store, usually an expression in the columns of this are involved.",
394
                        "store - data store from which values will be collected"
395
                    },
396
                    "Object"
397
            );
398
        }
399

    
400
        @Override
401
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
402
            String expression_s = getStr(args, 0);
403
            FeatureStore store = (FeatureStore) getObject(args, 1);
404
            try {
405
                FeatureSet set = store.getFeatureSelection();
406
                Feature feature = set.first();
407
                if (feature == null) {
408
                    return null;
409
                }
410
                DataManager dataManager = DALLocator.getDataManager();
411
                FeatureSymbolTable symbolTable = dataManager.createFeatureSymbolTable();
412
                symbolTable.setFeature(feature);
413

    
414
                ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
415
                Expression expression = expManager.createExpression();
416
                expression.setPhrase(expression_s);
417
                Object value = expression.execute(symbolTable.createParent());
418
                
419
                return value;
420
            } catch (Exception ex) {
421
                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_SELECTED_NAME+"' function", ex);
422
            }
423
        }
424
    }
425

    
426
    private class ViewFunction extends AbstractFunction {
427

    
428
        public ViewFunction() {
429
            super(
430
                    PROJECT_GROUP,
431
                    "view",
432
                    Range.is(1),
433
                    "Access to the indicated view",
434
                    "view({{viewName}})",
435
                    new String[]{
436
                        "view - String value with the name of a view"
437
                    },
438
                    "DocumentView"
439
            );
440
        }
441

    
442
        @Override
443
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
444
            String viewName = getStr(args, 0);
445
            Project project = currentProject.get();
446
            ViewDocument view = (ViewDocument) project.getDocument(viewName, ViewManager.TYPENAME);
447
            return view;
448
        }
449

    
450
    }
451

    
452
    private class ViewBBoxFunction extends AbstractFunction {
453

    
454
        public ViewBBoxFunction() {
455
            super(
456
                    PROJECT_GROUP,
457
                    "viewbbox",
458
                    Range.is(0),
459
                    "Return the BBox of the active view.",
460
                    "viewbbox()",
461
                    null,
462
                    "Geometry"
463
            );
464
        }
465

    
466
        @Override
467
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
468
            return currentViewEnvelope.get();
469
        }
470

    
471
    }
472

    
473

    
474
    private class SavedPointFunction extends AbstractFunction {
475

    
476
        public SavedPointFunction() {
477
            super(
478
                    PROJECT_GROUP,
479
                    "savedpoint",
480
                    Range.is(1),
481
                    "Return the value of the saved point with the name indicated.\n"
482
                            + "If the named point do not exists return null.",
483
                    "savedpoint({{name}})",
484
                    new String[]{
485
                        "name - String value with the name of the point"
486
                    },
487
                    "Geometry"
488
            );
489
        }
490

    
491
        @Override
492
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
493
            TemporaryStorageManager manager = TemporaryStorageLocator.getTemporaryStorageManager();
494
            TemporaryStorageGroup storage = manager.create("Points",Point.class);
495
            Geometry value = (Geometry) storage.get(getStr(args, 0));
496
            return value;
497
        }
498

    
499
    }
500

    
501
    private  class AreaFunction extends AbstractGeometryFunction {
502
        
503
        public AreaFunction() {
504
            super(
505
                    PROJECT_GROUP,
506
                    AREA_NAME,
507
                    Range.between(2,3),
508
                    "Calculate the area of the geometry in the indicated units",
509
                    AREA_NAME+"({{geometry}}, 'm?')",
510
                    new String[]{
511
                        "geometry - Geometry",
512
                        "Projection - Optional. Projection of the geometry or a string with the projection name",
513
                        "units - String value with the name the units to use"
514
                    },
515
                    "Double"
516
            );
517
        }
518

    
519
        @Override
520
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
521
            int units = 0;
522
            IProjection proj = null;
523
            Geometry geom = getGeom(args, 0);
524
            switch( args.length ) {
525
                case 2:
526
                    units = getUnitsArea(args, 1);
527
                    break;
528
                case 3:
529
                    proj = getProjection(args, 1);
530
                    units = getUnitsArea(args, 2);
531
                    break;
532
            }
533
            if (proj == null) {
534
                proj = geom.getProjection();
535
            }
536
            AreaAndPerimeterCalculator calculator = new AreaAndPerimeterCalculator();
537
            double area = calculator.area(geom, proj, units);
538
            return area;
539
        }
540
    }
541

    
542
    private class PerimeterFunction extends AbstractGeometryFunction {
543

    
544
        public PerimeterFunction() {
545
            super(
546
                    PROJECT_GROUP,
547
                    PERIMETER_NAME,
548
                    Range.between(2,3),
549
                    "Calculate the perimeter of the geometry in the indicated units",
550
                    PERIMETER_NAME+"({{geometry}}, 'm')",
551
                    new String[]{
552
                        "geometry - Geometry",
553
                        "Projection - Optional. Projection of the geometry or a string with the projection name",
554
                        "units - String value with the name the units to use"
555
                    },
556
                    "Double"
557
            );
558
        }
559

    
560
        @Override
561
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
562
            int units = 0;
563
            IProjection proj = null;
564
            Geometry geom = getGeom(args, 0);
565
            switch( args.length ) {
566
                case 2:
567
                    units = getUnitsArea(args, 1);
568
                    break;
569
                case 3:
570
                    proj = getProjection(args, 1);
571
                    units = getUnitsArea(args, 2);
572
                    break;
573
            }
574
            if (proj == null) {
575
                proj = geom.getProjection();
576
            }
577
            AreaAndPerimeterCalculator calculator = new AreaAndPerimeterCalculator();
578
            double area = calculator.perimeter(geom, proj, units);
579
            return area;
580
        }
581
    }
582

    
583
    private class PropertyFunction extends AbstractFunction {
584

    
585
        public PropertyFunction() {
586
            super(
587
                    PROJECT_GROUP,
588
                    "property",
589
                    Range.between(2, 3),
590
                    "Access to a property value in a properties file. If the"
591
                    + "indicated filename is not absolute, access it relative"
592
                    + "to the project.",
593
                    "property({{filename}}, name, defaultValue)",
594
                    new String[]{
595
                        "filename - String value with the name of the properties file",
596
                        "name - String value with the name of the property to retrieve from the file",
597
                        "defaultValue - Optional. Default value if can't access the file or the property",},
598
                    "String"
599
            );
600
        }
601

    
602
        @Override
603
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
604
            String filename = getStr(args, 0);
605
            String name = getStr(args, 1);
606
            String defaultValue = null;
607
            if (args.length == 3) {
608
                defaultValue = getStr(args, 2);
609
            }
610

    
611
            File file = new File(filename);
612
            if (!file.isAbsolute()) {
613
                Project project = currentProject.get();
614
                if (project.getFile() == null) {
615
                    return defaultValue;
616
                }
617
                file = new File(project.getFile().getParent(), filename);
618
            }
619
            Map<File, Properties> x = propertiesFiles.get();
620
            Properties properties = x.get(file);
621
            if (properties == null) {
622
                properties = new Properties();
623
                FileInputStream in = null;
624
                try {
625
                    in = new FileInputStream(file);
626
                    properties.load(in);
627
                } catch (Exception ex) {
628
                    return defaultValue;
629
                } finally {
630
                    IOUtils.closeQuietly(in);
631
                }
632
                x.put(file, properties);
633
            }
634
            String value = properties.getProperty(name, defaultValue);
635
            return value;
636
        }
637

    
638
    }
639

    
640
    ProjectValue currentProject = new ProjectValue();
641
    CurrentViewValue currentView = new CurrentViewValue();
642
    CurrentViewEnvelopeValue currentViewEnvelope = new CurrentViewEnvelopeValue();
643
    PropertiesValue propertiesFiles = new PropertiesValue();
644

    
645
    @SuppressWarnings("OverridableMethodCallInConstructor")
646
    public ProjectSymbolTable() {
647
        super(NAME);
648
        this.addFunction(new CurrentProjectFunction());
649
        this.addFunction(new StoreFunction());
650
        this.addFunction(new FetchFunction());
651
        this.addFunction(new FetchFirstFunction());
652
        this.addFunction(new FetchFirstSelectedFunction());
653
        this.addFunction(new ViewFunction());
654
        this.addFunction(new ViewBBoxFunction());
655
        this.addFunction(new PropertyFunction());
656
        this.addFunction(new AreaFunction());
657
        this.addFunction(new PerimeterFunction());
658
        this.addFunction(new SavedPointFunction());
659
        this.addFunction(new ApplicationFunction());
660
    }
661

    
662
    private MapContext getMapContext(Feature feature) {
663
        FeatureStore store = feature.getStore();
664
        Project project = ProjectManager.getInstance().getCurrentProject();
665
        project.getDocuments(ViewManager.TYPENAME);
666
        for (Document document : project.getDocuments(ViewManager.TYPENAME)) {
667
            ViewDocument view = (ViewDocument) document;
668
            MapContext mapContext = view.getMapContext();
669
            FLayer layer = getLayer(mapContext, store);
670
            if (layer != null) {
671
                return mapContext;
672
            }
673
        }
674
        return null;
675
    }
676

    
677
    private FLayer getLayer(MapContext mapContext, FeatureStore store) {
678
        Iterator<FLayer> it = mapContext.deepiterator();
679
        while (it.hasNext()) {
680
            FLayer layer = it.next();
681
            if (layer instanceof FLyrVect) {
682
                FeatureStore layer_store = ((FLyrVect) layer).getFeatureStore();
683
                if (layer_store == store) {
684
                    return layer;
685
                }
686
            }
687
        }
688
        return null;
689
    }
690

    
691
    private IProjection getProjection(Object[] args, int i) {
692
        Object value = args[i];
693
        if (value == null) {
694
            return null;
695
        }
696
        if (value instanceof IProjection) {
697
            return (IProjection) value;
698
        }
699
        String code = value.toString();
700
        return CRSFactory.getCRS(code);
701
    }
702

    
703
    private int getUnitsArea(Object[] args, int i) {
704
        Object value = args[i];
705
        if (value == null) {
706
            throw new IllegalArgumentException("Illegal unit value 'null'");
707
        }
708
        if (value instanceof Number) {
709
            return ((Number) value).intValue();
710
        }
711
        String name = value.toString();
712
        String[] names = MapContext.getAreaAbbr();
713
        for (int j = 0; j < names.length; j++) {
714
            if (name.equalsIgnoreCase(names[j])) {
715
                return j;
716
            }
717
        }
718
        names = MapContext.getAreaNames();
719
        for (int j = 0; j < names.length; j++) {
720
            if (name.equalsIgnoreCase(names[j])) {
721
                return j;
722
            }
723
        }
724
        throw new IllegalArgumentException("Illegal unit name '" + name + "'");
725
    }
726

    
727
    private int getUnitsDistance(Object[] args, int i) {
728
        Object value = args[i];
729
        if (value == null) {
730
            throw new IllegalArgumentException("Illegal unit value 'null'");
731
        }
732
        if (value instanceof Number) {
733
            return ((Number) value).intValue();
734
        }
735
        String name = value.toString();
736
        String[] names = MapContext.getDistanceAbbr();
737
        for (int j = 0; j < names.length; j++) {
738
            if (name.equalsIgnoreCase(names[j])) {
739
                return j;
740
            }
741
        }
742
        names = MapContext.getDistanceNames();
743
        for (int j = 0; j < names.length; j++) {
744
            if (name.equalsIgnoreCase(names[j])) {
745
                return j;
746
            }
747
        }
748
        throw new IllegalArgumentException("Illegal unit name '" + name + "'");
749
    }
750

    
751
}