Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / ShapeMFactory.java @ 24154

History | View | Annotate | Download (5.44 KB)

1
package com.iver.cit.gvsig.fmap.core;
2

    
3
import java.nio.ByteBuffer;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.Vector;
7

    
8

    
9
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
10
 *
11
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
12
 *
13
 * This program is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU General Public License
15
 * as published by the Free Software Foundation; either version 2
16
 * of the License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
26
 *
27
 * For more information, contact:
28
 *
29
 *  Generalitat Valenciana
30
 *   Conselleria d'Infraestructures i Transport
31
 *   Av. Blasco Ib??ez, 50
32
 *   46010 VALENCIA
33
 *   SPAIN
34
 *
35
 *      +34 963862235
36
 *   gvsig@gva.es
37
 *      www.gvsig.gva.es
38
 *
39
 *    or
40
 *
41
 *   IVER T.I. S.A
42
 *   Salamanca 50
43
 *   46005 Valencia
44
 *   Spain
45
 *
46
 *   +34 963163400
47
 *   dac@iver.es
48
 */
49
/* CVS MESSAGES:
50
 *
51
 * $Id: ShapeMFactory.java,v 1.1 2007/10/19 10:03:45 jorpiell Exp $
52
 * $Log: ShapeMFactory.java,v $
53
 * Revision 1.1  2007/10/19 10:03:45  jorpiell
54
 * First commit
55
 *
56
 *
57
 */
58
/**
59
 * This factory is used to create geoemtries with the M 
60
 * coordinate
61
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
62
 */
63
public class ShapeMFactory {
64
        
65
        public static IGeometry createPoint2DM(double x, double y, double m) {
66
                return new FGeometryM(new FPoint2DM(x, y, m));
67
        }
68
        
69
        
70
        /**
71
         * Creates a Polyline in 2D with the M coordinate
72
         * @param gp
73
         * Coordinates to create the polyline
74
         * @param ms
75
         * Array with the M values
76
         * @return
77
         * A Geometry with Ms
78
         */
79
        public static IGeometryM createPolyline2DM(GeneralPathX gp, double[] ms) {
80
                return new FGeometryM(new FPolyline2DM(gp, ms));
81
        }
82
        
83
        public static IGeometry createPolyline2DM(ByteBuffer data) {
84
                
85
                int count = data.getInt();
86
        GeneralPathX gp = new GeneralPathX();
87
        //double[] ms = new double[count - 1];
88
        //ArrayList alMs = new ArrayList();
89
        
90
        double[] ms = null;      //Intento de evitar el tener que encapsular las m's en
91
        double[] ms_aux = null;  //objetos Double y de tener que recorrer un ArrayList
92
        int ms_lentgh = 0;
93
        
94
        for (int i=0; i < count; i++) {
95
                parseTypeAndSRID(data);
96
            FPoint2DM[] points = parsePointArray(data);
97
            ms_aux = new double[ms_lentgh + points.length];
98
            
99
            gp.moveTo(points[0].getX(), points[0].getY());
100
            //alMs.add(new Double(points[0].getM()));
101
            ms_aux[ms_lentgh + 0] = points[0].getM();
102
            
103
            for (int j = 1; j< points.length; j++) {
104
                    //alMs.add(new Double(points[j].getM()));
105
                    ms_aux[ms_lentgh + j] = points[j].getM();
106
                gp.lineTo(points[j].getX(), points[j].getY());
107
            } 
108
            
109
            //ms[i] = points[i].getM();
110
            if (ms != null) {
111
                    System.arraycopy(ms, 0, ms_aux, ms.length, ms.length);
112
            }
113
            ms = ms_aux;
114
            ms_lentgh = ms.length;
115
            ms_aux = null;
116
        }//for
117
                
118
        /*
119
        // OJO: Para ahorrarme esto tendr?a que modificar la clase FPolyline2DM para
120
        //      que las ms se almacenaran como objetos Double en lugar de usar el tipo
121
        //      primitivo double.
122
        double[] ms = new double[alMs.size()];
123
        for (int i = 0; i < alMs.size(); i++) {
124
                ms[i] = ((Double)alMs.get(i)).doubleValue();
125
        }
126
        */
127
        return new FGeometryM(new FPolyline2DM(gp, ms));
128
        }
129
        
130
        public static IGeometry createPolygon2DM(GeneralPathX shape, double[] pM) {
131
                throw new UnsupportedOperationException();
132
        }
133
        
134
        
135
        public static IGeometry createMultipoint2DM(double[] x, double[] y,
136
                        double[] m) {
137
                        throw new UnsupportedOperationException();
138
                }
139
        
140
    private static void parseTypeAndSRID(ByteBuffer data) {
141
        byte endian = data.get(); //skip and test endian flag
142
        /* if (endian != data.endian) {
143
            throw new IllegalArgumentException("Endian inconsistency!");
144
        } */
145
        int typeword = data.getInt();
146
        int realtype = typeword & 0x1FFFFFFF; //cut off high flag bits
147

    
148
        boolean gHaveZ = (typeword & 0x80000000) != 0;
149
        boolean gHaveM = (typeword & 0x40000000) != 0;
150
        boolean gHaveS = (typeword & 0x20000000) != 0;
151

    
152
        int srid = -1;
153

    
154
        if (gHaveS) {
155
            srid = data.getInt();
156
        }
157
        
158
    }
159
    
160
    /**
161
     * Parse an Array of "slim" Points (without endianness and type, part of
162
     * LinearRing and Linestring, but not MultiPoint!
163
     * 
164
     * @param haveZ
165
     * @param haveM
166
     */
167
    private static FPoint2DM[] parsePointArray(ByteBuffer data) {
168
        int count = data.getInt();
169
        FPoint2DM[] result = new FPoint2DM[count];
170
        for (int i = 0; i < count; i++) {
171
            result[i] = parsePoint(data);
172
        }
173
        return result;
174
    }
175
    
176
    private static FPoint2DM parsePoint(ByteBuffer data) {
177
        double X = data.getDouble();
178
        double Y = data.getDouble();
179
        double M = data.getDouble();
180

    
181
        return new FPoint2DM(X, Y, M);
182
    }   
183
    
184
    
185
}