Revision 2669 branches/CqCMSDvp/libraries/libCq CMS for java.old/src/org/cresques/geo/cover/Mtn.java

View differences:

Mtn.java
1 1
/*
2 2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
5 3
 *
4
 * Copyright (C) 2004-5.
5
 *
6 6
 * This program is free software; you can redistribute it and/or
7 7
 * modify it under the terms of the GNU General Public License
8 8
 * as published by the Free Software Foundation; either version 2
......
18 18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19 19
 *
20 20
 * For more information, contact:
21
 * 
21
 *
22 22
 * cresques@gmail.com
23 23
 */
24 24
package org.cresques.geo.cover;
25 25

  
26
import java.io.BufferedReader;
27
import java.io.FileNotFoundException;
28
import java.io.FileReader;
29
import java.io.IOException;
30
import java.lang.Character;
31
import java.util.TreeMap;
32
import java.util.Iterator;
33
import java.util.Vector;
34

  
35 26
import org.cresques.geo.Ellipsoid;
36 27
import org.cresques.geo.UtmZone;
28

  
37 29
import org.cresques.px.Extent;
38 30
import org.cresques.px.IObjList;
39 31
import org.cresques.px.Minuteo;
......
43 35
import org.cresques.px.dxf.DxfSolid;
44 36
import org.cresques.px.dxf.DxfText;
45 37

  
38
import java.io.BufferedReader;
39
import java.io.FileNotFoundException;
40
import java.io.FileReader;
41
import java.io.IOException;
42

  
43
import java.lang.Character;
44

  
45
import java.util.Iterator;
46
import java.util.TreeMap;
47
import java.util.Vector;
48

  
49

  
46 50
/**
47 51
 * Calculos de hojas del Instituto Geogr?fico Nacional de Espa?a.
48
 * 
52
 *
49 53
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
50 54
 */
51

  
52 55
class Ign {
53
	/* MTN50: En coordenadas geogr?ficas: ancho = 
54
	 * 
55
	 */
56
    /* MTN50: En coordenadas geogr?ficas: ancho =
57
     *
58
     */
56 59
}
57 60

  
61

  
58 62
/**
59 63
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
60 64
 */
61 65
public class Mtn {
62
	public static Mtn25 mtn25 = new Mtn25();
63
	static Mtn50 mtn50 = Mtn.mtn25.getMtn50();
64
	public static TreeMap hoja25 = Mtn.mtn25.hoja;
65
	public static TreeMap hoja50 = Mtn.mtn50.hoja;
66
	
67
	UtmZone zone = null;
68
	String name = null;
69
	TreeMap hoja = null;
70
	private Minuteo min = null;
71
	
72
	public Minuteo getMinuteo() { return min; }
73
	
74
	protected void calcMinuteo() {
75
		System.out.println(name+": genero Minuteo");
76
		min = new Minuteo(zone);
77
		String hName;
78
		PxContour contour;
79
		Iterator keys;
80
		Hoja h = null;
66
    public static Mtn25 mtn25 = new Mtn25();
67
    static Mtn50 mtn50 = Mtn.mtn25.getMtn50();
68
    public static TreeMap hoja25 = Mtn.mtn25.hoja;
69
    public static TreeMap hoja50 = Mtn.mtn50.hoja;
70
    UtmZone zone = null;
71
    String name = null;
72
    TreeMap hoja = null;
73
    private Minuteo min = null;
81 74

  
82
		keys = hoja.keySet().iterator();
83
		while (keys.hasNext()) {
84
			hName = (String) keys.next();
85
			h = (Hoja) hoja.get(hName);
86
			contour = new PxContour(h.getVertex(), hName);
87
			contour.setColor(null);
88
			min.add(contour);
89
		}
90
	}
91
	
92
	public static void parseLayer(PxObjList layer) {
93
		Mtn25.mtn25.parseLyr(layer);
94
	}
95
	public static Minuteo getMinuteo25() { return mtn25.getMinuteo(); }
96
	public static Minuteo getMinuteo50() { return mtn50.getMinuteo(); }
75
    public Minuteo getMinuteo() {
76
        return min;
77
    }
78

  
79
    protected void calcMinuteo() {
80
        System.out.println(name + ": genero Minuteo");
81
        min = new Minuteo(zone);
82

  
83
        String hName;
84
        PxContour contour;
85
        Iterator keys;
86
        Hoja h = null;
87

  
88
        keys = hoja.keySet().iterator();
89

  
90
        while (keys.hasNext()) {
91
            hName = (String) keys.next();
92
            h = (Hoja) hoja.get(hName);
93
            contour = new PxContour(h.getVertex(), hName);
94
            contour.setColor(null);
95
            min.add(contour);
96
        }
97
    }
98

  
99
    public static void parseLayer(PxObjList layer) {
100
        Mtn25.mtn25.parseLyr(layer);
101
    }
102

  
103
    public static Minuteo getMinuteo25() {
104
        return mtn25.getMinuteo();
105
    }
106

  
107
    public static Minuteo getMinuteo50() {
108
        return mtn50.getMinuteo();
109
    }
97 110
}
98 111

  
112

  
99 113
class Mtn50 extends Mtn {
100
	public Mtn50() {
101
		name = "MTN50";
102
		zone = UtmZone.getProjection(Ellipsoid.hayford, 30, UtmZone.NORTH);
103
		hoja = new TreeMap();
104
	}
114
    public Mtn50() {
115
        name = "MTN50";
116
        zone = UtmZone.getProjection(Ellipsoid.hayford, 30, UtmZone.NORTH);
117
        hoja = new TreeMap();
118
    }
105 119
}
106 120

  
121

  
107 122
class Mtn25 extends Mtn {
108
	String buf;
109
	//private TreeMap hojas25 = new TreeMap();
123
    String buf;
110 124

  
111
	BufferedReader fi;
112
	long l = 0;
113
	
114
	public Mtn25() {
115
		name = "MTN25";
116
		zone = UtmZone.getProjection(Ellipsoid.hayford, 30, UtmZone.NORTH);
117
		hoja = new TreeMap();
118
		/*String fname = "data/mtn25.txt";
119
		load(fname);
120
		calcMinuteo();*/
121
	}
122
	
123
	public Mtn50 getMtn50() {
124
		Mtn50 mtn50 = new Mtn50();
125
		
126
		Iterator keys = hoja.keySet().iterator();
127
		Hoja h = null, h50 = null;
128
		String code50;
129
		while (keys.hasNext()) {
130
			h = (Hoja) hoja.get(keys.next());
131
			//System.err.println("Hoja 25"+h.code);
132
			code50 = h.code.split("-")[0];
133
			if (mtn50.hoja.containsKey(code50)) {
134
				h50 = (Hoja) mtn50.hoja.get(code50);
135
				switch (Integer.parseInt(h.code.split("-")[1])) {
136
				case 1: h50.setTL(h.tl); break;	
137
				case 2: h50.setTR(h.tr); break;	
138
				case 3: h50.setBL(h.bl); break;	
139
				case 4: h50.setBR(h.br); break;	
140
				}
141
			} else
142
				h50 = new Hoja(code50, h, null);
143
			mtn50.hoja.put(code50, h50);		
144
		}
145
		mtn50.calcMinuteo();
146
		return mtn50;
147
	}
125
    //private TreeMap hojas25 = new TreeMap();
126
    BufferedReader fi;
127
    long l = 0;
148 128

  
149
	public void load(String name) {
150
		try {
151
			System.out.println("Cargando '"+name+"' ...");
152
			fi = new BufferedReader(new FileReader(name));
153
			while ((buf = fi.readLine()) != null) {
154
				//System.out.println(buf);
155
				l++;
156
				parseLine(buf);
157
			}
158
			fi.close();
159
			System.out.println("'"+name+"' cargado. ("+l+" l?neas).");
160
		} catch (FileNotFoundException e) {
161
			e.printStackTrace();
162
		} catch (IOException ie) {
163
			System.err.println("ERROR."+l+"lineas leidas");
164
			ie.printStackTrace();
165
		}
166
	}
167
	
168
	void parseLine(String l) {
169
		//756-1,[(311114.0, 4337768.0), (325515.0, 4337434.0), (325309.0, 4328185.0), (310891.0, 4328519.0)]
170
		String [] field = l.split(",");
171
		field[1] = field[1].substring(1);
172
		field[8] = field[8].substring(0,field[8].length()-1);
173
		String name = field[0];
174
		String aux;
175
		double x,y;
176
		Vector pt = new Vector(); 
177
		for (int i=1; i<8; i+=2) {
178
			x = Double.parseDouble(field[i].trim().substring(1));
179
			aux = field[i+1].trim();
180
			y = Double.parseDouble(aux.substring(0, aux.length()-1));
181
			pt.add(zone.createPoint(x, y));
182
		}
183
		hoja.put(name, new Hoja(name, pt, ""));
184
		System.out.println(name);
185
	}
186
	
187
	/*
188
	 * Genera las hojas del MTN25 a partir de una capa dxf.
189
	 * Llena un TreeMap con las hojas, de manera que se pueden
190
	 * obtener por su c?digo.
191
	 */
192
	private void fillSolids(IObjList marcos, IObjList oList) {
193
		Iterator iter = oList.iterator();;
194
		while (iter.hasNext()) {
195
			Object o = iter.next();
196
			if (o instanceof IObjList) fillSolids(marcos, (IObjList) o);
197
			else if (o instanceof DxfSolid) marcos.add((PxObj) o);
198
		}
199
	}
200
	
201
	private void fillTexts(IObjList textos, IObjList oList) {
202
		Iterator iter = oList.iterator();;
203
		while (iter.hasNext()) {
204
			Object o = iter.next();
205
			if (o instanceof IObjList) fillTexts(textos, (IObjList) o);
206
			else if (o instanceof DxfText)
207
				//if (Character.isDigit(((DxfText) o).getText().charAt(1)))
208
				textos.add((Extent.Has) o);
209
		}
210
	}
211
	
212
	public void parseLyr(IObjList layer) {
213
		hoja = new TreeMap();
214
		PxObjList marcos = new PxObjList();
215
		PxObjList textos = new PxObjList();
216
		Iterator iter = null;
217
		// Lleno una lista con solids, otra con texts.
218
		System.out.println("layer: "+layer.size()+" objetos ("+layer.iterator().next()+")");
219
		fillSolids(marcos, layer);
220
		System.out.println("fillSolids: "+marcos.size()+" marcos");
221
		fillTexts(textos, layer);
222
		System.out.println("fillTexts: "+textos.size()+" textos");
223
		// Para cada text busco un solid. Si lo encuentro creo
224
		// una hoja con el solid y el text como name, la mento en
225
		// el TreeMap y saco el solid de marcos
226
		Hoja h = null;
227
		DxfText text = null;
228
		DxfSolid marco = null;
229
		IObjList select = null;
230
		iter = textos.iterator();
231
		while (iter.hasNext()) {
232
			text = (DxfText) iter.next();
233
			if (hoja.containsKey(text.getText())) continue;
234
			if (!Character.isDigit(text.getText().split("-")[1].charAt(0))) continue;
235
			select = marcos.getAt(text.getPt1());
236
			if (select.size() >= 1) {
237
				marco = (DxfSolid) select.iterator().next();
238
				h = new Hoja(text.getText(), marco.getPts(), null);
239
				h.setProjection(layer.getProjection());
240
				hoja.put(text.getText(), h);
241
				marcos.remove(marco);
242
			} else {
243
				// ERROR. No puede haber m?s de un marco por texto 
244
			}
245
		}
246
		
247
		calcMinuteo();
248
		Mtn.mtn50 = Mtn.mtn25.getMtn50();
249
		Mtn.hoja25 = Mtn.mtn25.hoja;
250
		Mtn.hoja50 = Mtn.mtn50.hoja;
251
	}
129
    public Mtn25() {
130
        name = "MTN25";
131
        zone = UtmZone.getProjection(Ellipsoid.hayford, 30, UtmZone.NORTH);
132
        hoja = new TreeMap();
133

  
134
        /*String fname = "data/mtn25.txt";
135
        load(fname);
136
        calcMinuteo();*/
137
    }
138

  
139
    public Mtn50 getMtn50() {
140
        Mtn50 mtn50 = new Mtn50();
141

  
142
        Iterator keys = hoja.keySet().iterator();
143
        Hoja h = null;
144
        Hoja h50 = null;
145
        String code50;
146

  
147
        while (keys.hasNext()) {
148
            h = (Hoja) hoja.get(keys.next());
149

  
150
            //System.err.println("Hoja 25"+h.code);
151
            code50 = h.code.split("-")[0];
152

  
153
            if (mtn50.hoja.containsKey(code50)) {
154
                h50 = (Hoja) mtn50.hoja.get(code50);
155

  
156
                switch (Integer.parseInt(h.code.split("-")[1])) {
157
                case 1:
158
                    h50.setTL(h.tl);
159

  
160
                    break;
161

  
162
                case 2:
163
                    h50.setTR(h.tr);
164

  
165
                    break;
166

  
167
                case 3:
168
                    h50.setBL(h.bl);
169

  
170
                    break;
171

  
172
                case 4:
173
                    h50.setBR(h.br);
174

  
175
                    break;
176
                }
177
            } else {
178
                h50 = new Hoja(code50, h, null);
179
            }
180

  
181
            mtn50.hoja.put(code50, h50);
182
        }
183

  
184
        mtn50.calcMinuteo();
185

  
186
        return mtn50;
187
    }
188

  
189
    public void load(String name) {
190
        try {
191
            System.out.println("Cargando '" + name + "' ...");
192
            fi = new BufferedReader(new FileReader(name));
193

  
194
            while ((buf = fi.readLine()) != null) {
195
                //System.out.println(buf);
196
                l++;
197
                parseLine(buf);
198
            }
199

  
200
            fi.close();
201
            System.out.println("'" + name + "' cargado. (" + l + " l?neas).");
202
        } catch (FileNotFoundException e) {
203
            e.printStackTrace();
204
        } catch (IOException ie) {
205
            System.err.println("ERROR." + l + "lineas leidas");
206
            ie.printStackTrace();
207
        }
208
    }
209

  
210
    void parseLine(String l) {
211
        //756-1,[(311114.0, 4337768.0), (325515.0, 4337434.0), (325309.0, 4328185.0), (310891.0, 4328519.0)]
212
        String[] field = l.split(",");
213
        field[1] = field[1].substring(1);
214
        field[8] = field[8].substring(0, field[8].length() - 1);
215

  
216
        String name = field[0];
217
        String aux;
218
        double x;
219
        double y;
220
        Vector pt = new Vector();
221

  
222
        for (int i = 1; i < 8; i += 2) {
223
            x = Double.parseDouble(field[i].trim().substring(1));
224
            aux = field[i + 1].trim();
225
            y = Double.parseDouble(aux.substring(0, aux.length() - 1));
226
            pt.add(zone.createPoint(x, y));
227
        }
228

  
229
        hoja.put(name, new Hoja(name, pt, ""));
230
        System.out.println(name);
231
    }
232

  
233
    /*
234
     * Genera las hojas del MTN25 a partir de una capa dxf.
235
     * Llena un TreeMap con las hojas, de manera que se pueden
236
     * obtener por su c?digo.
237
     */
238
    private void fillSolids(IObjList marcos, IObjList oList) {
239
        Iterator iter = oList.iterator();
240
        ;
241

  
242
        while (iter.hasNext()) {
243
            Object o = iter.next();
244

  
245
            if (o instanceof IObjList) {
246
                fillSolids(marcos, (IObjList) o);
247
            } else if (o instanceof DxfSolid) {
248
                marcos.add((PxObj) o);
249
            }
250
        }
251
    }
252

  
253
    private void fillTexts(IObjList textos, IObjList oList) {
254
        Iterator iter = oList.iterator();
255
        ;
256

  
257
        while (iter.hasNext()) {
258
            Object o = iter.next();
259

  
260
            if (o instanceof IObjList) {
261
                fillTexts(textos, (IObjList) o);
262
            } else if (o instanceof DxfText) {
263
                //if (Character.isDigit(((DxfText) o).getText().charAt(1)))
264
                textos.add((Extent.Has) o);
265
            }
266
        }
267
    }
268

  
269
    public void parseLyr(IObjList layer) {
270
        hoja = new TreeMap();
271

  
272
        PxObjList marcos = new PxObjList();
273
        PxObjList textos = new PxObjList();
274
        Iterator iter = null;
275

  
276
        // Lleno una lista con solids, otra con texts.
277
        System.out.println("layer: " + layer.size() + " objetos (" +
278
                           layer.iterator().next() + ")");
279
        fillSolids(marcos, layer);
280
        System.out.println("fillSolids: " + marcos.size() + " marcos");
281
        fillTexts(textos, layer);
282
        System.out.println("fillTexts: " + textos.size() + " textos");
283

  
284
        // Para cada text busco un solid. Si lo encuentro creo
285
        // una hoja con el solid y el text como name, la mento en
286
        // el TreeMap y saco el solid de marcos
287
        Hoja h = null;
288
        DxfText text = null;
289
        DxfSolid marco = null;
290
        IObjList select = null;
291
        iter = textos.iterator();
292

  
293
        while (iter.hasNext()) {
294
            text = (DxfText) iter.next();
295

  
296
            if (hoja.containsKey(text.getText())) {
297
                continue;
298
            }
299

  
300
            if (!Character.isDigit(text.getText().split("-")[1].charAt(0))) {
301
                continue;
302
            }
303

  
304
            select = marcos.getAt(text.getPt1());
305

  
306
            if (select.size() >= 1) {
307
                marco = (DxfSolid) select.iterator().next();
308
                h = new Hoja(text.getText(), marco.getPts(), null);
309
                h.setProjection(layer.getProjection());
310
                hoja.put(text.getText(), h);
311
                marcos.remove(marco);
312
            } else {
313
                // ERROR. No puede haber m?s de un marco por texto 
314
            }
315
        }
316

  
317
        calcMinuteo();
318
        Mtn.mtn50 = Mtn.mtn25.getMtn50();
319
        Mtn.hoja25 = Mtn.mtn25.hoja;
320
        Mtn.hoja50 = Mtn.mtn50.hoja;
321
    }
252 322
}
253

  

Also available in: Unified diff