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 @ 46091

History | View | Annotate | Download (28.6 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.Map;
8
import java.util.Properties;
9
import org.apache.commons.io.IOUtils;
10
import org.apache.commons.lang3.Range;
11
import org.apache.commons.lang3.StringUtils;
12
import org.cresques.cts.IProjection;
13
import org.gvsig.app.ApplicationLocator;
14
import org.gvsig.app.ApplicationManager;
15
import org.gvsig.app.project.Project;
16
import org.gvsig.app.project.ProjectManager;
17
import org.gvsig.app.project.documents.Document;
18
import org.gvsig.app.project.documents.view.ViewDocument;
19
import org.gvsig.app.project.documents.view.ViewManager;
20
import org.gvsig.expressionevaluator.Expression;
21
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
22
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
23
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
24
import org.gvsig.expressionevaluator.Interpreter;
25
import org.gvsig.expressionevaluator.spi.AbstractFunction;
26
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
27
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
28
import org.gvsig.fmap.crs.CRSFactory;
29
import org.gvsig.fmap.dal.DALLocator;
30
import org.gvsig.fmap.dal.DataManager;
31
import org.gvsig.fmap.dal.HasDataStore;
32
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureSet;
35
import org.gvsig.fmap.dal.feature.FeatureStore;
36
import org.gvsig.fmap.geom.Geometry;
37
import org.gvsig.fmap.geom.primitive.Point;
38
import org.gvsig.fmap.mapcontext.MapContext;
39
import org.gvsig.fmap.mapcontext.layers.FLayer;
40
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
41
import org.gvsig.fmap.mapcontrol.AreaAndPerimeterCalculator;
42
import org.gvsig.temporarystorage.TemporaryStorageGroup;
43
import org.gvsig.temporarystorage.TemporaryStorageLocator;
44
import org.gvsig.temporarystorage.TemporaryStorageManager;
45

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

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

    
68
        T value = null;
69
        long lastAccess = 0;
70

    
71
        protected abstract void reload();
72

    
73
        public boolean isExpired() {
74
            long now = System.currentTimeMillis();
75
            return now - lastAccess > 3000;
76
        }
77

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

    
87
    private class ProjectValue extends CachedValue<Project> {
88

    
89
        @Override
90
        protected void reload() {
91
            value = ProjectManager.getInstance().getCurrentProject();
92
        }
93

    
94
    }
95

    
96
    private class CurrentViewValue extends CachedValue<ViewDocument> {
97

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

    
105
    }
106

    
107
    private class CurrentViewEnvelopeValue extends CachedValue<Geometry> {
108

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

    
120
    }
121

    
122
    private class PropertiesValue extends CachedValue<Map<File, Properties>> {
123

    
124
        @Override
125
        protected void reload() {
126
            value = new HashMap<>();
127
        }
128
    }
129

    
130
    private class CurrentProjectFunction extends AbstractFunction {
131

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

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

    
149
    }
150

    
151
    private class ApplicationFunction extends AbstractFunction {
152

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

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

    
170
    }
171

    
172
    private class TabbleFunction extends AbstractFunction {
173

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

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

    
211
                case 2:
212
                    ViewDocument view;
213
                    String viewName = getStr(args, 0);
214
                    if( StringUtils.isBlank(viewName) ) {
215
                        view = (ViewDocument) project.getActiveDocument(ViewManager.TYPENAME);
216
                        if (view == null) {
217
                            throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't locate active View.");
218
//                            return null;
219
                        }
220
                    } else {
221
                        view = (ViewDocument) project.getDocument(viewName, ViewManager.TYPENAME);
222
                        if (view == null) {
223
                            throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't locate View '"+viewName+"'.");
224
//                            return null;
225
                        }
226
                    }
227
                    String layerName = getStr(args, 1);
228
                    FLayer layer = view.getLayer(layerName);
229
                    if( layer == null ) {
230
                        throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't locate layer '"+layerName+"' in View '"+viewName+"'.");
231
                    }
232
                    if( layer instanceof HasDataStore ) {
233
                        return ((HasDataStore)layer).getDataStore();
234
                    }
235
                    throw new ExpressionRuntimeException("Problems calling '"+TABLE_NAME+"' function, can't get the store from layer '"+layerName+"' in View '"+viewName+"'.");
236
//                    return null;
237
            }
238
            return null;
239
        }
240
        
241
    }
242
    
243
//    private class FetchFunction extends AbstractFunction {
244
//
245
//        public FetchFunction() {
246
//            super(
247
//                    PROJECT_GROUP,
248
//                    FETCH_NAME,
249
//                    Range.between(2, 4),
250
//                    "Access to the features of a table and retuen a list with the values.",
251
//                    FETCH_NAME+"({{value}}, store, where, order)",
252
//                    new String[]{
253
//                        "value - value to retrieve from the store, usually an expression in the columns of this are involved.",
254
//                        "store - data store from which values will be collected",
255
//                        "where - Optional. String value with a filter expression",
256
//                        "order - Optional. String value with the order. must be a string with the names of separate fields with commas"
257
//                    },
258
//                    "Object"
259
//            );
260
//        }
261
//
262
//        @Override
263
//        public Object call(Interpreter interpreter, Object[] args) throws Exception {
264
//            Object value;
265
//            String where = null;
266
//            String order = null;
267
//            String expression_s = getStr(args, 0);
268
//            FeatureStore store = (FeatureStore) getObject(args, 1);
269
//            switch (args.length) {
270
//                case 3:
271
//                    where = getStr(args, 2);
272
//                    break;
273
//                case 4:
274
//                    where = getStr(args, 2);
275
//                    order = getStr(args, 3);
276
//                    break;
277
//            }
278
//            try {
279
//                List<Feature> features;
280
//                if (where == null && order == null) {
281
//                    features = store.getFeatures();
282
//                } else {
283
//                    FeatureQuery query = store.createFeatureQuery();
284
//                    if (where != null) {
285
//                        query.addFilter(where);
286
//                    }
287
//                    if (order != null) {
288
//                        query.getOrder().add(order);
289
//                    }
290
//                    query.retrievesAllAttributes();
291
//                    features = store.getFeatures(query);
292
//                }
293
//                UnmodifiableBasicList<Object> list = new ListOfFeaturesWrapper(
294
//                        features,
295
//                        interpreter.getSymbolTable(), 
296
//                        expression_s
297
//                );
298
//                return list;
299
//            } catch (Exception ex) {
300
//                throw new ExpressionRuntimeException("Problems calling '"+FETCH_NAME+"' function", ex);
301
//            }
302
//        }
303
//    }
304
//
305
//    private class FetchFirstFunction extends AbstractFunction {
306
//
307
//        public FetchFirstFunction() {
308
//            super(
309
//                    PROJECT_GROUP,
310
//                    FETCH_FIRST_NAME,
311
//                    Range.between(2, 4),
312
//                    "Access to the first feature of the layer, and "
313
//                    + "return the value of the attribute.",
314
//                    FETCH_FIRST_NAME+"({{value}}, store, where, order)",
315
//                    new String[]{
316
//                        "value - value to retrieve from the store, usually an expression in the columns of this are involved.",
317
//                        "store - data store from which values will be collected",
318
//                        "where - Optional. String value with a filter expression",
319
//                        "order - Optional. String value with the order. must be a string with the names of separate fields with commas"
320
//                    },
321
//                    "Object"
322
//            );
323
//        }
324
//
325
//        @Override
326
//        public Object call(Interpreter interpreter, Object[] args) throws Exception {
327
//            Object value;
328
//            String where = null;
329
//            String order = null;
330
//            String expression_s = getStr(args, 0);
331
//            FeatureStore store = (FeatureStore) getObject(args, 1);
332
//            if( store == null ) {
333
//                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_NAME+"' function, the store is null.");
334
//            }
335
//            switch (args.length) {
336
//                case 3:
337
//                    where = getStr(args, 2);
338
//                    break;
339
//                case 4:
340
//                    where = getStr(args, 2);
341
//                    order = getStr(args, 3);
342
//                    break;
343
//            }
344
//            try {
345
//                FeatureSet set;
346
//                if (where == null && order == null) {
347
//                    set = store.getFeatureSet();
348
//                } else {
349
//                    FeatureQuery query = store.createFeatureQuery();
350
//                    if (where != null) {
351
//                        query.addFilter(where);
352
//                    }
353
//                    if (order != null) {
354
//                        query.getOrder().add(order);
355
//                    }
356
//                    query.retrievesAllAttributes();
357
//                    set = store.getFeatureSet(query);
358
//                }
359
//                Feature feature = set.first();
360
//                if (feature == null) {
361
//                    return null;
362
//                }
363
//                DataManager dataManager = DALLocator.getDataManager();
364
//                FeatureSymbolTable symbolTable = dataManager.createFeatureSymbolTable();
365
//                symbolTable.setFeature(feature);
366
//
367
//                ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
368
//                Expression expression = expManager.createExpression();
369
//                expression.setPhrase(expression_s);
370
//                value = expression.execute(symbolTable.createParent());
371
//                
372
//                return value;
373
//            } catch (Exception ex) {
374
//                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_NAME+"' function", ex);
375
//            }
376
//        }
377
//    }
378

    
379
    private class FetchFirstSelectedFunction extends AbstractFunction {
380

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

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

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

    
422
    private class ViewFunction extends AbstractFunction {
423

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

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

    
446
    }
447

    
448
    private class ViewBBoxFunction extends AbstractFunction {
449

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

    
462
        @Override
463
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
464
            return currentViewEnvelope.get();
465
        }
466

    
467
    }
468

    
469

    
470
    private class SavedPointFunction extends AbstractFunction {
471

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

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

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

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

    
538
    private class PerimeterFunction extends AbstractGeometryFunction {
539

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

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

    
579
    private class PropertyFunction extends AbstractFunction {
580

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

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

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

    
634
    }
635

    
636
    ProjectValue currentProject = new ProjectValue();
637
    CurrentViewValue currentView = new CurrentViewValue();
638
    CurrentViewEnvelopeValue currentViewEnvelope = new CurrentViewEnvelopeValue();
639
    PropertiesValue propertiesFiles = new PropertiesValue();
640

    
641
    @SuppressWarnings("OverridableMethodCallInConstructor")
642
    public ProjectSymbolTable() {
643
        super(NAME);
644
        this.addFunction(new CurrentProjectFunction());
645
        this.addFunction(new TabbleFunction());
646
//        this.addFunction(new FetchFunction());
647
//        this.addFunction(new FetchFirstFunction());
648
        this.addFunction(new FetchFirstSelectedFunction());
649
        this.addFunction(new ViewFunction());
650
        this.addFunction(new ViewBBoxFunction());
651
        this.addFunction(new PropertyFunction());
652
        this.addFunction(new AreaFunction());
653
        this.addFunction(new PerimeterFunction());
654
        this.addFunction(new SavedPointFunction());
655
        this.addFunction(new ApplicationFunction());
656
    }
657

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

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

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

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

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

    
747
}