Revision 822 org.gvsig.projection.jcrs/trunk/org.gvsig.projection.jcrs/org.gvsig.projection.jcrs.lib/src/main/java/org/gvsig/crs/CrsFactory.java
CrsFactory.java | ||
---|---|---|
315 | 315 |
logger.debug("Can't import from ESRI WKT.", e); |
316 | 316 |
return null; |
317 | 317 |
} |
318 |
try { |
|
319 |
sr.AutoIdentifyEPSG(); |
|
320 |
String authorityCode = sr.GetAuthorityCode(null); |
|
321 |
if (!StringUtils.isEmpty(authorityCode)) { |
|
322 |
logger.debug("AuthorityCode: " + authorityCode); |
|
323 |
return new Crs(sr); |
|
324 |
} |
|
325 |
} catch (Exception e) { |
|
326 |
logger.debug("Can't AutoIdentifyEPSG.", e); |
|
327 |
} |
|
328 |
boolean bProj4; |
|
329 |
try { |
|
330 |
@SuppressWarnings("unused") |
|
331 |
String proj4 = sr.ExportToProj4(); |
|
332 |
bProj4 = true; |
|
333 |
} catch (Exception e2) { |
|
334 |
logger.debug("Can't ExportToProj4.", e2); |
|
335 |
bProj4 = false; |
|
336 |
} |
|
337 |
int code; |
|
338 |
try { |
|
339 |
code = identifyEPSG(sr, bProj4); |
|
340 |
if ((code == 0) && (bProj4 == true)) { |
|
341 |
code = identifyESRI(sr); |
|
342 |
if (code == -1) { |
|
343 |
code = identifyEPSG(sr, false); |
|
344 |
if (code == 0) { |
|
345 |
return null; |
|
346 |
} |
|
347 |
} |
|
348 |
} |
|
349 |
} catch (SQLException e3) { |
|
350 |
e3.printStackTrace(); |
|
318 |
SpatialReference sr2 = getFixedSpatiaReference(sr); |
|
319 |
if( sr2==null ) { |
|
351 | 320 |
return null; |
352 | 321 |
} |
353 |
SpatialReference sr2 = new SpatialReference(); |
|
354 |
try { |
|
355 |
sr2.ImportFromEPSG(code); |
|
356 |
return new Crs(sr2); |
|
357 |
} catch (Exception e4) { |
|
358 |
logger.warn("Can't find the best candidate.", e4); |
|
359 |
return null; |
|
360 |
} |
|
322 |
return new Crs(sr); |
|
323 |
|
|
361 | 324 |
} else if (ICRSFactory.FORMAT_WKT.equalsIgnoreCase(format)) { |
362 |
SpatialReference sr = new SpatialReference(); |
|
363 |
sr.ImportFromWkt(value); |
|
325 |
SpatialReference sr = getSpatialReferenceFromWKT(value); |
|
364 | 326 |
return new Crs(sr); |
365 | 327 |
|
366 | 328 |
} else if (ICRSFactory.FORMAT_PROJ4.equalsIgnoreCase(format)) { |
... | ... | |
370 | 332 |
} |
371 | 333 |
return null; |
372 | 334 |
} |
335 |
|
|
336 |
public static SpatialReference getSpatialReferenceFromWKT(String wkt) { |
|
337 |
SpatialReference sr = new SpatialReference(); |
|
338 |
sr.ImportFromWkt(wkt); |
|
339 |
SpatialReference sr2 = getFixedSpatiaReference(sr); |
|
340 |
if( sr2==null ) { |
|
341 |
return sr; |
|
342 |
} |
|
343 |
return sr2; |
|
344 |
} |
|
373 | 345 |
|
374 |
public int identifyEPSG(SpatialReference sr, boolean bProj4) throws SQLException { |
|
346 |
private static SpatialReference getFixedSpatiaReference(SpatialReference sr) { |
|
347 |
try { |
|
348 |
sr.AutoIdentifyEPSG(); |
|
349 |
String authorityCode = sr.GetAuthorityCode(null); |
|
350 |
if (!StringUtils.isEmpty(authorityCode)) { |
|
351 |
logger.debug("AuthorityCode: " + authorityCode); |
|
352 |
return sr; |
|
353 |
} |
|
354 |
} catch (Exception e) { |
|
355 |
logger.debug("Can't AutoIdentifyEPSG.", e); |
|
356 |
} |
|
357 |
boolean bProj4; |
|
358 |
try { |
|
359 |
@SuppressWarnings("unused") |
|
360 |
String proj4 = sr.ExportToProj4(); |
|
361 |
bProj4 = true; |
|
362 |
} catch (Exception e2) { |
|
363 |
logger.debug("Can't ExportToProj4.", e2); |
|
364 |
bProj4 = false; |
|
365 |
} |
|
366 |
int code; |
|
367 |
try { |
|
368 |
code = identifyEPSG(sr, bProj4); |
|
369 |
if ((code == 0) && (bProj4 == true)) { |
|
370 |
code = identifyESRI(sr); |
|
371 |
if (code == -1) { |
|
372 |
code = identifyEPSG(sr, false); |
|
373 |
if (code == 0) { |
|
374 |
return null; |
|
375 |
} |
|
376 |
} |
|
377 |
} |
|
378 |
} catch (SQLException e3) { |
|
379 |
return null; |
|
380 |
} |
|
381 |
SpatialReference sr2 = new SpatialReference(); |
|
382 |
try { |
|
383 |
sr2.ImportFromEPSG(code); |
|
384 |
return sr2; |
|
385 |
} catch (Exception e4) { |
|
386 |
logger.warn("Can't find the best candidate.", e4); |
|
387 |
return null; |
|
388 |
} |
|
389 |
} |
|
390 |
|
|
391 |
private static int identifyEPSG(SpatialReference sr, boolean bProj4) throws SQLException { |
|
375 | 392 |
|
376 | 393 |
int best_code = 0; |
377 | 394 |
int best_dist = 99; |
... | ... | |
450 | 467 |
return best_code; |
451 | 468 |
} |
452 | 469 |
|
453 |
public int identifyESRI(SpatialReference sr) throws SQLException {
|
|
470 |
private static int identifyESRI(SpatialReference sr) throws SQLException {
|
|
454 | 471 |
|
455 | 472 |
int code0 = -1; |
456 | 473 |
|
... | ... | |
496 | 513 |
return code0; |
497 | 514 |
} |
498 | 515 |
|
499 |
public String normalizeESRI(String sr_name) {
|
|
516 |
private static String normalizeESRI(String sr_name) {
|
|
500 | 517 |
String name; |
501 | 518 |
name = StringUtils.replace(sr_name, "GCS_", ""); |
502 | 519 |
name = StringUtils.replaceChars(name, "()", ""); |
... | ... | |
542 | 559 |
return name; |
543 | 560 |
} |
544 | 561 |
|
545 |
public String normalizeEPSG(String sr_name) {
|
|
562 |
private static String normalizeEPSG(String sr_name) {
|
|
546 | 563 |
String name; |
547 | 564 |
name = StringUtils.replace(sr_name, " / ", " "); |
548 | 565 |
name = StringUtils.replace(name, "-", " "); |
Also available in: Unified diff