root / tags / v1_1_Build_1007 / libraries / libCq CMS for java.old / src / org / cresques / io / MapServerClient.java @ 12478
History | View | Annotate | Download (4.05 KB)
1 | 2809 | nacho | /*
|
---|---|---|---|
2 | * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
||
3 | *
|
||
4 | * Copyright (C) 2004-5.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * cresques@gmail.com
|
||
23 | */
|
||
24 | package org.cresques.io; |
||
25 | |||
26 | import org.cresques.px.Extent; |
||
27 | |||
28 | import java.io.BufferedInputStream; |
||
29 | import java.io.IOException; |
||
30 | import java.io.InputStream; |
||
31 | |||
32 | import java.net.MalformedURLException; |
||
33 | import java.net.URL; |
||
34 | |||
35 | |||
36 | /**
|
||
37 | * Consulta para un servidor de mapas.
|
||
38 | * @author Luis W. Sevilla (sevilla_lui@gva.es)
|
||
39 | */
|
||
40 | public abstract class MapServerClient implements Extent.Has { |
||
41 | public static final int MAXBUFSIZE = 1024 * 1024; |
||
42 | BufferedInputStream bufIn = null; |
||
43 | byte[] buffer; |
||
44 | int bufPos;
|
||
45 | String serverName;
|
||
46 | protected String urlBase = null; |
||
47 | protected long xMin; |
||
48 | protected long xMax; |
||
49 | protected long yMin; |
||
50 | protected long yMax; |
||
51 | protected int ancho; |
||
52 | protected int alto; |
||
53 | public boolean echo = false; |
||
54 | private Extent extent = null; |
||
55 | private int maxViewWidth = 1500; |
||
56 | private int maxViewHeight = 1500; |
||
57 | private float minTimeBetweenQuerys = 0; |
||
58 | |||
59 | public MapServerClient(String serverName) { |
||
60 | this.serverName = serverName;
|
||
61 | } |
||
62 | |||
63 | public String getName() { |
||
64 | return serverName;
|
||
65 | } |
||
66 | |||
67 | public byte[] getBuffer() { |
||
68 | return buffer;
|
||
69 | } |
||
70 | |||
71 | public void setView(long xMin, long yMin, long xMax, long yMax) { |
||
72 | this.xMin = xMin;
|
||
73 | this.xMax = xMax;
|
||
74 | this.yMax = yMax;
|
||
75 | this.yMin = yMin;
|
||
76 | } |
||
77 | |||
78 | public void setViewSize(int w, int h) { |
||
79 | ancho = w; |
||
80 | alto = h; |
||
81 | } |
||
82 | |||
83 | /**
|
||
84 | * Pone el tama?o m?ximo de la vista. Solicitando una vista m?s
|
||
85 | * grande que esta al servidor dar? error (y no im?genes) :-(
|
||
86 | * @param w ancho
|
||
87 | * @param h alto
|
||
88 | */
|
||
89 | public void setMaxViewSize(int w, int h) { |
||
90 | maxViewWidth = w; |
||
91 | maxViewHeight = h; |
||
92 | } |
||
93 | |||
94 | public Extent getViewExtent() {
|
||
95 | return new Extent(xMin, yMax, xMax, yMin); |
||
96 | } |
||
97 | |||
98 | public void setExtent(Extent e) { |
||
99 | extent = e; |
||
100 | } |
||
101 | |||
102 | public Extent getExtent() {
|
||
103 | return extent;
|
||
104 | } |
||
105 | |||
106 | abstract public String getUrl(); |
||
107 | |||
108 | public void get() throws Exception { |
||
109 | get(getUrl()); |
||
110 | } |
||
111 | |||
112 | public void get(String address) throws Exception { |
||
113 | bufPos = 0;
|
||
114 | buffer = new byte[MAXBUFSIZE]; |
||
115 | |||
116 | try {
|
||
117 | // Check to see that a command parameter was entered
|
||
118 | // Create an URL instance
|
||
119 | URL url = new URL(address); |
||
120 | |||
121 | // Get an input stream for reading
|
||
122 | InputStream in = url.openStream();
|
||
123 | |||
124 | // Create a buffered input stream for efficency
|
||
125 | bufIn = new BufferedInputStream(in); |
||
126 | |||
127 | // Repeat until end of file
|
||
128 | int data;
|
||
129 | |||
130 | for (bufPos = 0; (data = bufIn.read()) != -1; bufPos++) { |
||
131 | buffer[bufPos] = (byte) data;
|
||
132 | |||
133 | if (echo) {
|
||
134 | System.out.println(data);
|
||
135 | } |
||
136 | } |
||
137 | |||
138 | byte[] buf = new byte[bufPos]; |
||
139 | |||
140 | for (int i = 0; i < bufPos; i++) |
||
141 | buf[i] = buffer[i]; |
||
142 | |||
143 | buffer = buf; |
||
144 | System.out.println(bufPos + " bytes leidos."); |
||
145 | |||
146 | bufIn.close(); |
||
147 | in.close(); |
||
148 | } catch (MalformedURLException mue) { |
||
149 | System.err.println("Invalid URL"); |
||
150 | } catch (IOException ioe) { |
||
151 | System.err.println("I/O Error - " + ioe); |
||
152 | } |
||
153 | } |
||
154 | } |