Skip to content

Commit

Permalink
if srid is absent from pe_list_projcs_geogcs esri file, use srid2prj …
Browse files Browse the repository at this point in the history
…wkt resource file
  • Loading branch information
mukoki committed Aug 25, 2022
1 parent 6899249 commit aa9b253
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 14 deletions.
29 changes: 17 additions & 12 deletions src/com/vividsolutions/jump/coordsys/EsriProj.java
Expand Up @@ -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 <a href="https://github.com/Esri/projection-engine-db-doc">Esri Github Repo</a>,
* licensed under the Apache License, Version 2.0 (the "License").
*
* This class uses the two following files from the github repository :
Expand All @@ -33,16 +33,19 @@
public class EsriProj {

// A cache to remember already used projection string
public static Map<Integer,String> PROJMAP = new HashMap<>();
public static final Map<Integer,String> PROJMAP = new HashMap<>();
// A cache to remember already used projection ids
public static Map<String,Integer> CODEMAP = new HashMap<>();
//public static final Map<String,Integer> 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));
}
Expand Down Expand Up @@ -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));

Expand Down
12 changes: 10 additions & 2 deletions src/com/vividsolutions/jump/io/ShapefileWriter.java
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions 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);
}
}

0 comments on commit aa9b253

Please sign in to comment.