From aa9b253f93ba0a9a61b3f3174d40893c02576d7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Michaud?= Date: Thu, 25 Aug 2022 08:48:25 +0200 Subject: [PATCH] if srid is absent from pe_list_projcs_geogcs esri file, use srid2prj wkt resource file --- .../jump/coordsys/EsriProj.java | 29 ++++++++------- .../jump/io/ShapefileWriter.java | 12 +++++-- .../jump/io/ShapefileWriterTest.java | 36 +++++++++++++++++++ 3 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 test/com/vividsolutions/jump/io/ShapefileWriterTest.java diff --git a/src/com/vividsolutions/jump/coordsys/EsriProj.java b/src/com/vividsolutions/jump/coordsys/EsriProj.java index bb0863ab6..db8c8a49e 100644 --- a/src/com/vividsolutions/jump/coordsys/EsriProj.java +++ b/src/com/vividsolutions/jump/coordsys/EsriProj.java @@ -3,15 +3,15 @@ import com.vividsolutions.jump.io.CompressedFile; import com.vividsolutions.jump.workbench.JUMPWorkbench; import com.vividsolutions.jump.workbench.Logger; -import org.apache.commons.compress.archivers.ArchiveException; import java.io.*; +import java.net.URISyntaxException; import java.util.*; /** * This class is used to find the precise Esri prj string for a given srid. * - * Prj strings are taken from https://github.com/Esri/projection-engine-db-doc, + * Prj strings are taken from Esri Github Repo, * licensed under the Apache License, Version 2.0 (the "License"). * * This class uses the two following files from the github repository : @@ -33,16 +33,19 @@ public class EsriProj { // A cache to remember already used projection string - public static Map PROJMAP = new HashMap<>(); + public static final Map PROJMAP = new HashMap<>(); // A cache to remember already used projection ids - public static Map CODEMAP = new HashMap<>(); + //public static final Map CODEMAP = new HashMap<>(); - private static final File projfile = JUMPWorkbench.getInstance().getPlugInManager() - .findFileOrFolderInExtensionDirs("coord_ref_sys/pe_list_projcs_geogcs.zip"); - private static final String entryName = "pe_list_projcs_geogcs.csv"; + private static File projfile; + private static String entryName; + public static void setProjFile(File zipFileName, String zipEntryName) { + projfile = zipFileName; + entryName = zipEntryName; + } - public static void main(String[] args) throws IOException, ArchiveException { + public static void main(String[] args) throws IOException, URISyntaxException { System.out.println(findProj(2154)); System.out.println(findProj(2154)); } @@ -71,13 +74,15 @@ private static String[] tokenize(String line) { return tokens.toArray(new String[0]); } - public static String findProj(final int id) throws IOException, ArchiveException { + public static String findProj(final int id) throws IOException { //long t0 = System.currentTimeMillis(); String proj = PROJMAP.get(id); - if (proj == null) proj = PROJMAP.get(id); if (proj == null) { - //InputStream fis = new FileInputStream(projfile); - //InputStream is = CompressedFile.getUncompressedStream(fis, entryName); + if (projfile == null) { + projfile = JUMPWorkbench.getInstance().getPlugInManager() + .findFileOrFolderInExtensionDirs("coord_ref_sys/pe_list_projcs_geogcs.zip"); + entryName = "pe_list_projcs_geogcs.csv"; + } InputStream is = CompressedFile.openFile(projfile.getPath(), entryName); BufferedReader br = new BufferedReader(new InputStreamReader(is)); diff --git a/src/com/vividsolutions/jump/io/ShapefileWriter.java b/src/com/vividsolutions/jump/io/ShapefileWriter.java index 41c0713f3..3d8a6abd1 100644 --- a/src/com/vividsolutions/jump/io/ShapefileWriter.java +++ b/src/com/vividsolutions/jump/io/ShapefileWriter.java @@ -47,6 +47,7 @@ import org.geotools.dbffile.DbfFile; import org.geotools.dbffile.DbfFileWriter; import org.geotools.shapefile.Shapefile; +import org.openjump.core.ccordsys.utils.SridLookupTable; import javax.swing.*; import java.io.*; @@ -693,10 +694,17 @@ else if (columnType == AttributeType.STRING || } - private String getPrjString(String code) throws Exception { + protected String getPrjString(String code) throws Exception { try { int srid = Integer.parseInt(code); - return EsriProj.findProj(srid); + String prj = EsriProj.findProj(srid); + // TODO SridLookupTable can find prj which are not available in EsriProj + // but it returns WKT which maybe slightly different from esri prj + // https://gis.stackexchange.com/questions/355184/prj-files-from-esri-arent-wkt + if (prj == null) { + prj = SridLookupTable.getOGCWKTFromWkidCode(""+srid); + } + return prj; } catch(IOException e) { Logger.warn(e); return null; diff --git a/test/com/vividsolutions/jump/io/ShapefileWriterTest.java b/test/com/vividsolutions/jump/io/ShapefileWriterTest.java new file mode 100644 index 000000000..0e1100ac5 --- /dev/null +++ b/test/com/vividsolutions/jump/io/ShapefileWriterTest.java @@ -0,0 +1,36 @@ +package com.vividsolutions.jump.io; + +import com.vividsolutions.jump.coordsys.EsriProj; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; + + +public class ShapefileWriterTest { + + + @Test + public void testFindProjInPe_list_projcs_geogcs() throws Exception { + EsriProj.setProjFile(new File("lib/ext/coord_ref_sys/pe_list_projcs_geogcs.zip"), + "pe_list_projcs_geogcs.csv"); + String proj = new ShapefileWriter().getPrjString("3147"); + // String proj = EsriProj.findProj(3147); + System.out.println(proj); + Assert.assertNotNull(proj); + } + + @Test + public void testFindProjInPe_list_projcs_geogc2() throws Exception { + EsriProj.setProjFile(new File("lib/ext/coord_ref_sys/pe_list_projcs_geogcs.zip"), + "pe_list_projcs_geogcs.csv"); + String proj = new ShapefileWriter().getPrjString("2046"); + // EsriProj.setProjFile(new File("lib/ext/coord_ref_sys/pe_list_projcs_geogcs.zip"), + // "pe_list_projcs_geogcs.csv"); + // String proj = EsriProj.findProj(2046); + System.out.println(proj); + // 2046 is not in pe_list_projcs_geogcs.csv but can be found from alternative file + // srid2prj.txt by EsriProj + Assert.assertNotNull(proj); + } +}