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 44129 jjdelcerro
package org.gvsig.app.project.symboltables;
2 43987 jjdelcerro
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.util.HashMap;
6 43997 jjdelcerro
import java.util.Iterator;
7 44203 jjdelcerro
import java.util.List;
8 43987 jjdelcerro
import java.util.Map;
9
import java.util.Properties;
10
import org.apache.commons.io.IOUtils;
11
import org.apache.commons.lang3.Range;
12 44243 jjdelcerro
import org.apache.commons.lang3.StringUtils;
13 43997 jjdelcerro
import org.cresques.cts.IProjection;
14 44025 jjdelcerro
import org.gvsig.app.ApplicationLocator;
15
import org.gvsig.app.ApplicationManager;
16 44129 jjdelcerro
import org.gvsig.app.project.Project;
17
import org.gvsig.app.project.ProjectManager;
18 43997 jjdelcerro
import org.gvsig.app.project.documents.Document;
19 43987 jjdelcerro
import org.gvsig.app.project.documents.view.ViewDocument;
20
import org.gvsig.app.project.documents.view.ViewManager;
21 44153 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
22
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
23
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
24 44189 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
25 43987 jjdelcerro
import org.gvsig.expressionevaluator.Interpreter;
26
import org.gvsig.expressionevaluator.spi.AbstractFunction;
27 44644 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractGeometryFunction;
28 43987 jjdelcerro
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
29 43997 jjdelcerro
import org.gvsig.fmap.crs.CRSFactory;
30 44153 jjdelcerro
import org.gvsig.fmap.dal.DALLocator;
31
import org.gvsig.fmap.dal.DataManager;
32 44189 jjdelcerro
import org.gvsig.fmap.dal.HasDataStore;
33 44153 jjdelcerro
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
34 43987 jjdelcerro
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 43997 jjdelcerro
import org.gvsig.fmap.geom.Geometry;
39 44035 jjdelcerro
import org.gvsig.fmap.geom.primitive.Point;
40 43997 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContext;
41 43987 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
42
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
43 43997 jjdelcerro
import org.gvsig.fmap.mapcontrol.AreaAndPerimeterCalculator;
44 44035 jjdelcerro
import org.gvsig.temporarystorage.TemporaryStorageGroup;
45
import org.gvsig.temporarystorage.TemporaryStorageLocator;
46
import org.gvsig.temporarystorage.TemporaryStorageManager;
47 44203 jjdelcerro
import org.gvsig.tools.util.UnmodifiableBasicList;
48 43987 jjdelcerro
49
/**
50
 *
51
 * @author jjdelcerro
52
 */
53
@SuppressWarnings("UseSpecificCatch")
54
public class ProjectSymbolTable extends AbstractSymbolTable {
55 44338 jjdelcerro
    public static final String PROJECT_GROUP = "Project";
56 43987 jjdelcerro
57 44129 jjdelcerro
    public static final String AREA_NAME = "AREA";
58
    public static final String PERIMETER_NAME = "PERIMETER";
59 44153 jjdelcerro
    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 44203 jjdelcerro
    public static final String FETCH_NAME = "FETCH";
63 44338 jjdelcerro
    public static final String APPLICATION_NAME = "APPLICATION";
64 44129 jjdelcerro
65
    static final String NAME = "Project";
66
67 44189 jjdelcerro
    private static final String TABLE_DOCUMENT_TYPENAME = "project.document.table";
68
69 43987 jjdelcerro
    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 44025 jjdelcerro
    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 43987 jjdelcerro
    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 44338 jjdelcerro
                    PROJECT_GROUP,
138 43987 jjdelcerro
                    "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 44338 jjdelcerro
    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 44153 jjdelcerro
    private class StoreFunction extends AbstractFunction {
176 44189 jjdelcerro
177 44153 jjdelcerro
        public StoreFunction() {
178
            super(
179 44338 jjdelcerro
                    PROJECT_GROUP,
180 44153 jjdelcerro
                    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 44243 jjdelcerro
                        "view - String value with the name of a view, if view is null use current View.",
191 44153 jjdelcerro
                        "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 43987 jjdelcerro
198 44153 jjdelcerro
        @Override
199
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
200
            Project project = currentProject.get();
201
            switch (args.length) {
202 44189 jjdelcerro
                case 1:
203
                    String tableName = getStr(args, 0);
204
                    Document document = project.getDocument(tableName, TABLE_DOCUMENT_TYPENAME);
205
                    if (document == null) {
206 44243 jjdelcerro
                        throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't locate table document '"+tableName+"'.");
207
//                        return null;
208 44189 jjdelcerro
                    }
209
                    if( document instanceof HasDataStore ) {
210
                        return ((HasDataStore) document).getDataStore();
211
                    }
212 44243 jjdelcerro
                    throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't get store from table document '"+tableName+"'.");
213
//                    return null;
214 44189 jjdelcerro
215 44153 jjdelcerro
                case 2:
216 44243 jjdelcerro
                    ViewDocument view;
217 44153 jjdelcerro
                    String viewName = getStr(args, 0);
218 44243 jjdelcerro
                    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 44153 jjdelcerro
                    String layerName = getStr(args, 1);
232 44243 jjdelcerro
                    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 44153 jjdelcerro
                    }
236 44189 jjdelcerro
                    if( layer instanceof HasDataStore ) {
237
                        return ((HasDataStore)layer).getDataStore();
238 44153 jjdelcerro
                    }
239 44243 jjdelcerro
                    throw new ExpressionRuntimeException("Problems calling '"+STORE_NAME+"' function, can't get the store from layer '"+layerName+"' in View '"+viewName+"'.");
240
//                    return null;
241 44153 jjdelcerro
            }
242
            return null;
243
        }
244
245
    }
246
247 44203 jjdelcerro
    private class FetchFunction extends AbstractFunction {
248
249
        public FetchFunction() {
250
            super(
251 44338 jjdelcerro
                    PROJECT_GROUP,
252 44203 jjdelcerro
                    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 44153 jjdelcerro
    private class FetchFirstFunction extends AbstractFunction {
310
311
        public FetchFirstFunction() {
312 43987 jjdelcerro
            super(
313 44338 jjdelcerro
                    PROJECT_GROUP,
314 44153 jjdelcerro
                    FETCH_FIRST_NAME,
315
                    Range.between(2, 4),
316 43987 jjdelcerro
                    "Access to the first feature of the layer, and "
317
                    + "return the value of the attribute.",
318 44153 jjdelcerro
                    FETCH_FIRST_NAME+"({{value}}, store, where, order)",
319 43987 jjdelcerro
                    new String[]{
320 44153 jjdelcerro
                        "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 43987 jjdelcerro
                    },
325
                    "Object"
326
            );
327
        }
328
329
        @Override
330
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
331 44189 jjdelcerro
            Object value;
332 44153 jjdelcerro
            String where = null;
333 43987 jjdelcerro
            String order = null;
334 44153 jjdelcerro
            String expression_s = getStr(args, 0);
335
            FeatureStore store = (FeatureStore) getObject(args, 1);
336 44243 jjdelcerro
            if( store == null ) {
337
                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_NAME+"' function, the store is null.");
338
            }
339 43997 jjdelcerro
            switch (args.length) {
340 44189 jjdelcerro
                case 3:
341 44153 jjdelcerro
                    where = getStr(args, 2);
342
                    break;
343 43987 jjdelcerro
                case 4:
344 44153 jjdelcerro
                    where = getStr(args, 2);
345
                    order = getStr(args, 3);
346 43987 jjdelcerro
                    break;
347
            }
348
            try {
349
                FeatureSet set;
350 44153 jjdelcerro
                if (where == null && order == null) {
351 43987 jjdelcerro
                    set = store.getFeatureSet();
352
                } else {
353
                    FeatureQuery query = store.createFeatureQuery();
354 44153 jjdelcerro
                    if (where != null) {
355
                        query.addFilter(where);
356 43987 jjdelcerro
                    }
357 43997 jjdelcerro
                    if (order != null) {
358 43987 jjdelcerro
                        query.getOrder().add(order);
359
                    }
360 44129 jjdelcerro
                    query.retrievesAllAttributes();
361 43987 jjdelcerro
                    set = store.getFeatureSet(query);
362
                }
363
                Feature feature = set.first();
364
                if (feature == null) {
365 44153 jjdelcerro
                    return null;
366 43987 jjdelcerro
                }
367 44153 jjdelcerro
                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 43987 jjdelcerro
            } catch (Exception ex) {
378 44189 jjdelcerro
                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_NAME+"' function", ex);
379 43987 jjdelcerro
            }
380
        }
381 44153 jjdelcerro
    }
382 43987 jjdelcerro
383 44153 jjdelcerro
    private class FetchFirstSelectedFunction extends AbstractFunction {
384
385
        public FetchFirstSelectedFunction() {
386 44338 jjdelcerro
            super(
387
                    PROJECT_GROUP,
388 44153 jjdelcerro
                    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 44189 jjdelcerro
                throw new ExpressionRuntimeException("Problems calling '"+FETCH_FIRST_SELECTED_NAME+"' function", ex);
422 44153 jjdelcerro
            }
423
        }
424 43987 jjdelcerro
    }
425
426
    private class ViewFunction extends AbstractFunction {
427
428
        public ViewFunction() {
429
            super(
430 44338 jjdelcerro
                    PROJECT_GROUP,
431 43987 jjdelcerro
                    "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 44025 jjdelcerro
    private class ViewBBoxFunction extends AbstractFunction {
453
454
        public ViewBBoxFunction() {
455
            super(
456 44338 jjdelcerro
                    PROJECT_GROUP,
457 44025 jjdelcerro
                    "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 44035 jjdelcerro
474
    private class SavedPointFunction extends AbstractFunction {
475
476
        public SavedPointFunction() {
477
            super(
478 44338 jjdelcerro
                    PROJECT_GROUP,
479 44035 jjdelcerro
                    "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 44644 jjdelcerro
    private  class AreaFunction extends AbstractGeometryFunction {
502 44129 jjdelcerro
503 43997 jjdelcerro
        public AreaFunction() {
504
            super(
505 44338 jjdelcerro
                    PROJECT_GROUP,
506 44129 jjdelcerro
                    AREA_NAME,
507 43999 jjdelcerro
                    Range.between(2,3),
508 43997 jjdelcerro
                    "Calculate the area of the geometry in the indicated units",
509 44129 jjdelcerro
                    AREA_NAME+"({{geometry}}, 'm?')",
510 43997 jjdelcerro
                    new String[]{
511
                        "geometry - Geometry",
512 43999 jjdelcerro
                        "Projection - Optional. Projection of the geometry or a string with the projection name",
513 43997 jjdelcerro
                        "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 43999 jjdelcerro
            int units = 0;
522
            IProjection proj = null;
523 43997 jjdelcerro
            Geometry geom = getGeom(args, 0);
524 43999 jjdelcerro
            switch( args.length ) {
525
                case 2:
526 44207 jjdelcerro
                    units = getUnitsArea(args, 1);
527 43999 jjdelcerro
                    break;
528
                case 3:
529
                    proj = getProjection(args, 1);
530 44207 jjdelcerro
                    units = getUnitsArea(args, 2);
531 43999 jjdelcerro
                    break;
532
            }
533 43997 jjdelcerro
            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 44644 jjdelcerro
    private class PerimeterFunction extends AbstractGeometryFunction {
543 43997 jjdelcerro
544
        public PerimeterFunction() {
545
            super(
546 44338 jjdelcerro
                    PROJECT_GROUP,
547 44129 jjdelcerro
                    PERIMETER_NAME,
548 43999 jjdelcerro
                    Range.between(2,3),
549 43997 jjdelcerro
                    "Calculate the perimeter of the geometry in the indicated units",
550 44129 jjdelcerro
                    PERIMETER_NAME+"({{geometry}}, 'm')",
551 43997 jjdelcerro
                    new String[]{
552
                        "geometry - Geometry",
553 43999 jjdelcerro
                        "Projection - Optional. Projection of the geometry or a string with the projection name",
554 43997 jjdelcerro
                        "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 43999 jjdelcerro
            int units = 0;
563
            IProjection proj = null;
564 43997 jjdelcerro
            Geometry geom = getGeom(args, 0);
565 43999 jjdelcerro
            switch( args.length ) {
566
                case 2:
567 44207 jjdelcerro
                    units = getUnitsArea(args, 1);
568 43999 jjdelcerro
                    break;
569
                case 3:
570
                    proj = getProjection(args, 1);
571 44207 jjdelcerro
                    units = getUnitsArea(args, 2);
572 43999 jjdelcerro
                    break;
573
            }
574 43997 jjdelcerro
            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 43987 jjdelcerro
    private class PropertyFunction extends AbstractFunction {
584
585
        public PropertyFunction() {
586
            super(
587 44338 jjdelcerro
                    PROJECT_GROUP,
588 43987 jjdelcerro
                    "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 44025 jjdelcerro
    CurrentViewValue currentView = new CurrentViewValue();
642
    CurrentViewEnvelopeValue currentViewEnvelope = new CurrentViewEnvelopeValue();
643 43987 jjdelcerro
    PropertiesValue propertiesFiles = new PropertiesValue();
644
645
    @SuppressWarnings("OverridableMethodCallInConstructor")
646
    public ProjectSymbolTable() {
647 44129 jjdelcerro
        super(NAME);
648 43987 jjdelcerro
        this.addFunction(new CurrentProjectFunction());
649 44153 jjdelcerro
        this.addFunction(new StoreFunction());
650 44203 jjdelcerro
        this.addFunction(new FetchFunction());
651 44153 jjdelcerro
        this.addFunction(new FetchFirstFunction());
652
        this.addFunction(new FetchFirstSelectedFunction());
653 43987 jjdelcerro
        this.addFunction(new ViewFunction());
654 44025 jjdelcerro
        this.addFunction(new ViewBBoxFunction());
655 43987 jjdelcerro
        this.addFunction(new PropertyFunction());
656 43997 jjdelcerro
        this.addFunction(new AreaFunction());
657
        this.addFunction(new PerimeterFunction());
658 44035 jjdelcerro
        this.addFunction(new SavedPointFunction());
659 44338 jjdelcerro
        this.addFunction(new ApplicationFunction());
660 43987 jjdelcerro
    }
661
662 43997 jjdelcerro
    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 44207 jjdelcerro
    private int getUnitsArea(Object[] args, int i) {
704 43997 jjdelcerro
        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 44207 jjdelcerro
    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 43987 jjdelcerro
}