logo资料库

java读取shp文件代码.doc

第1页 / 共14页
第2页 / 共14页
第3页 / 共14页
第4页 / 共14页
第5页 / 共14页
第6页 / 共14页
第7页 / 共14页
第8页 / 共14页
资料共14页,剩余部分请下载后查看
java读取ShapeFile文件的源代码
java 读取 ShapeFile 文件的源代码 时间:2010-04-20 22:20 来源: 作者:giser 点击: java 读取 ShapeFile 文件的源代码 70 次 java 读取 ShapeFile 文件的源代码 java.awt.Color; java.io.; java.net.URL; java.net.URLConnection; java.util.zip.; uk.ac.leeds.ccg.dbffile.DbfFileException; uk.ac.leeds.ccg.dbffile.DbfNet; uk.ac.leeds.ccg.shapefile.; class ShapefileReader sf; ShapefileReader(String baseFilename) name; dbf; idCol; int String Shapefile DbfNet import import import import import import import import 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. public 11. { 12. public 13. public 14. private 15. private 16. 17. public 18. { 19. super(); 20. sf 21. dbf 22. idCol 23. name 24. name 25. try 26. { 27. sf 28. } 29. catch(Exception 30. { 31. System.out.println(e); 32. } 33. } 34. 35. public 36. { 37. super(); none; baseFilename; = null; = null; = new = 3; = = Shapefile(baseFilename + .shp); e) ShapefileReader(URL base, int idCol)
= = = 3; none; null; null; = 38. sf 39. dbf 40. this.idCol 41. name 42. this.idCol 43. name 44. String 45. if(name.indexOf(63) = 46. sub idCol; sub = = = base.getFile(); = ; 0) name.substring(name.indexOf(63), name.lastIndexOf(47) ); sub); + shpZip dbfZip 47. System.out.println(Sub 48. boolean 49. boolean 50. try 51. { 52. System.out.println(SFR Looking = false; = false; for .zip version of + name); 53. String 54. String 55. System.out.println(No Ext 56. if(noExt.toLowerCase().endsWith(.shp) noExt.toLowerCase().en base.getFile(); ext noExt noExt); .zip; = = + dsWith(.zip)) name.substring(0, name.length() = 57. noExt 58. System.out.println(No Ext 59. URL zipURL = new URL(base.getProtocol(), + noExt); - 4); base.getHost(), + zipURL); = zipURL.openConnection(); base.getPort(), noExt + ext); = uc size 60. System.out.println(zip url 61. URLConnection 62. uc.setUseCaches(false); 63. uc.setDefaultUseCaches(false); 64. System.out.println(Opening zis); 65. int 66. if(size 67. { buf1[] 68. byte got[] 69. byte 70. InputStream 71. int 72. for(int new byte[size]; new byte[size 0; (grab total = is grab -1) != 0; = = = = = uc.getContentLength(); = uc.getInputStream(); 10]; is.read(got)) -1; total += grab) 73. System.arraycopy(got, 0, 74. 75. got null; = buf1, total, grab);
76. ByteArrayInputStream zipfile = new ByteArrayInputStream(b uf1); zis = 77. ZipInputStream 78. System.out.println(Looking for 79. ZipEntry 80. while((ze new ze; = ZipInputStream(zipfile); entries); zis.getNextEntry()) != null && (!shpZip !d bfZip)) + ze.getName()); 81. { 82. System.out.println(Found entry); 83. System.out.println(ze); 84. System.out.println(Entry 85. System.out.println(Getting entry); 86. if(ze.getName().toLowerCase().endsWith(.shp)) 87. { 88. name 89. System.out.println(Found .shp 90. size 91. byte 92. byte 93. int 94. for(int = buf[] in[] (int)ze.getSize(); new byte[size]; new byte[size]; zis.read(in)) ze.getName(); 0; (n index = 0; zip in = n = = = = file); -1; index += n) 95. System.arraycopy(in, 0, 96. 97. zis.closeEntry(); 98. ByteArrayInputStream bais ; buf, index, n); = new ByteArrayInputStream(buf) = = new true; Shapefile(bais); 99. sf 100. shpZip 101. } 102. else 103. if(ze.getName().toLowerCase().endsWith(.dbf)) 104. { 105. System.out.println(Found .dbf 106. size 107. byte 108. byte 109. int n; 110. for(int = buf[] in[] (int)ze.getSize(); new byte[size]; new byte[size]; zis.read(in)) 0; (n index zip in = = = = file); n) 111. System.arraycopy(in, 0, 112. 113. zis.closeEntry(); buf, index, n); -1; index +=
114. ByteArrayInputStream bais = new ByteArrayInputStream(buf) ; && true; new = dbfZip) DbfNet(bais); 115. dbf = 116. dbfZip 117. } 118. } 119. 120. if(shpZip 121. { 122. System.out.println(Zip file 123. return; 124. } 125. } 126. } 127. catch(ZipException ze) 128. { 129. System.err.println(SFR zip 130. } 131. catch(IOException 132. { 133. System.err.println(SFR zip _ex) version used + name); read failed + ze); read not possible, looking f ); and .dbf .shp or 134. } 135. catch(Exception 136. { 137. System.err.println(SFR General _ex) exception in zip read ); 138. } 139. System.out.println(Looking for files outside of zip fil e); 140. try 141. { 142. String 143. String 144. if(noExt.toLowerCase().endsWith(.shp) noExt.toLowerCase().en base.getFile(); ext noExt .shp; = = dsWith(.zip)) = name.substring(0, name.length() - 4); shapeURL = base.getPort(), new URL(base.getProtocol(), noExt ext); + base.getHost() , 145. noExt 146. URL 147. if(!shpZip) 148. sf 149. ext 150. URL = dbfURL new = .dbf; Shapefile(shapeURL); = new URL(base.getProtocol(), base.getHost(), base.getPort(), noExt + ext);
151. if(!dbfZip) = 152. dbf new 153. URL shxURL DbfNet(dbfURL); = new URL(base.getProtocol(), base.getHost(), base.getPort(), noExt + ext); e) 154. sf.readIndex(shxURL.openStream()); 155. } 156. catch(Exception 157. { 158. System.err.println(e); 159. } 160. } 161. 162. public int ShapefileReader(InputStream shpIn, idCol) InputStream dbf InputStream dbf = = = = 3; none; null; idCol; null; = In, 163. { 164. super(); 165. sf 166. dbf 167. this.idCol 168. name 169. this.idCol 170. try 171. { 172. sf 173. if(dbfIn 174. dbf 175. } 176. catch(Exception 177. { 178. System.err.println(e); 179. } 180. } 181. 182. public null) new new != = = e) Shapefile(shpIn); DbfNet(dbfIn); ShapefileReader(InputStream shpIn, = In) 183. { 184. super(); 185. sf 186. dbf 187. idCol 188. name 189. try 190. { 191. sf = = = = null; null; 3; none; new Shapefile(shpIn);
ShapefileReader(URL base) = new DbfNet(dbfIn); e) void guessIdCol() 0); != null) 192. if(dbfIn 193. dbf 194. } 195. catch(Exception 196. { 197. System.err.println(e); 198. } 199. guessIdCol(); 200. } 201. 202. public 203. { 204. this(base, 205. guessIdCol(); 206. } 207. 208. private 209. { 210. switch(sf.getShapeType()) 211. { 212. case 213. idCol 214. break; 215. 216. case 217. idCol 218. break; 219. 220. case 221. idCol 222. break; 223. 224. case 225. idCol 226. break; 227. 228. } 229. if(idCol 230. idCol = 231. System.out.println(ID col '005' '027' '003' '001' 23 = = 2; = 5; = 0; 5; 5 3 1 dbf.getNumFields() - 1) 0; guessed at + idCol + + dbf.getNumFields()); 232. } 233. 234. public void setIdCol(int col)
void setIdCol(String name) dbf.getFieldNumber(name); = 0 col idCol; int getShapeType() sf.getShapeType(); Theme getTheme() l = '005' null; = col; = col col = 5 readPolygons(); 235. { 236. idCol 237. } 238. 239. public 240. { 241. int 242. idCol 243. } 244. 245. public 246. { 247. return 248. } 249. 250. public 251. { 252. Layer 253. switch(getShapeType()) 254. { 255. case 256. l = 257. break; 258. 259. case 260. l = 261. break; 262. 263. case 264. l = 265. break; 266. 267. case 268. l = 269. break; 270. 271. default 272. return 273. 274. } 275. Theme new 276. t.setName(name); 277. return 278. } 1 readPoints(); '003' 3 readLines(); '027' 23 readLinesM(); '001' null; t; t = Theme(l);
dbf.getFieldNumber(colName); = = = t != col Theme null) getTheme(); 279. 280. public 281. { 282. Theme 283. if(colName 284. { 285. int 286. if(col 287. { 288. GeoData 289. shade.setRange(d); 290. t.setShader(shade); 291. t.setGeoData(d); 292. } 293. else 294. { 295. System.err.println(Column 0) d = readData(col); getTheme(Shader shade, String colName) + colName + not found in .dbf file); t; Theme getTheme(String colName, 296. } 297. } 298. return 299. } 300. 301. public 302. { 303. return 304. } 305. 306. public 307. { 308. LineLayer 309. uk.ac.leeds.ccg.shapefile.ShapefileShape polys[] getTheme(new satShader(c), new LineLayer(); readLines() colName); LineLayer Color map = hapes(); c) = sf.getS p[] = = = null; sf.getRecordCount(); 0; count; i++) i poly = (ShapeArc)polys[i]; nPoints nParts = poly.getNumPoints(); = poly.getNumParts(); = 0; j nParts; j++) 310. ShapePoint count 311. int 312. for(int i 313. { 314. ShapeArc 315. int 316. int 317. for(int 318. { 319. p 320. GeoPolygon j = poly.getPartPoints(j); gp = new GeoPolygon(i + 1, p);
分享到:
收藏