Skip to content

Instantly share code, notes, and snippets.

@csmoore
Created June 10, 2014 14:37
Show Gist options
  • Save csmoore/7e1d5f4329f7c1f9a19d to your computer and use it in GitHub Desktop.
Save csmoore/7e1d5f4329f7c1f9a19d to your computer and use it in GitHub Desktop.
Sample SymbolLookup & Loader Class
/*
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
*/
/**
* Encapsulates an entry for a Military Symbol
*/
public class MilitarySymbol {
private String name;
private String symbolId;
private String geometryType;
public MilitarySymbol(String name, String symbolId, String geometryType)
{
setName(name);
setSymbolId(symbolId);
setGeometryType(geometryType);
}
public void setName(String name) {
this.name = name;
}
public void setSymbolId(String symbolId) {
this.symbolId = symbolId.replace('*','-');
}
public void setGeometryType(String geom) {
this.geometryType = geom;
}
public String getName() {
return name;
}
public String getSymbolId() {
return symbolId;
}
public String getGeometryType() {
return geometryType;
}
public boolean isValidSymbol() {
if (name.equals(""))
return false;
if (symbolId.equals(""))
return false;
if (symbolId.length() == 15) {
return true;
}
else {
return false;
}
}
}
/*
| Licensed under the Apache License, Version 2.0 (the "License");
| you may not use this file except in compliance with the License.
| You may obtain a copy of the License at
|
| http://www.apache.org/licenses/LICENSE-2.0
|
| Unless required by applicable law or agreed to in writing, software
| distributed under the License is distributed on an "AS IS" BASIS,
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
| See the License for the specific language governing permissions and
| limitations under the License.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SymbolLookup {
public static String NOT_FOUND = "NOT FOUND";
private static Map<String, MilitarySymbol> sidc2SymbolProps = new HashMap<String, MilitarySymbol>();
private static Map<String, MilitarySymbol> symbolName2SymbolProps = new HashMap<String, MilitarySymbol>();
private static boolean initialized = false;
private static List<MilitarySymbol> symbolList = new ArrayList<MilitarySymbol>();
public SymbolLookup()
{
initialize();
}
private void initialize()
{
// we only want to do this once
if (initialized)
return;
initialized = true;
System.out.println("Initializing SymbolLookup");
try {
// TODO: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// TODO: figure out where this file is deployed:
// CSV File to use is here:
// https://raw.githubusercontent.com/Esri/joint-military-symbology-xml/master/source/JointMilitarySymbologyLibraryCS/TestLegacyCodesCsv/Data/SymbolInfo2525C.csv
String csvFile = "TestSymbolLookup/Data/SymbolInfo2525C.csv";
// TODO: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
File dataFile = new File(csvFile);
if (!dataFile.exists()) {
System.err.println ("ERROR: Could not find/open file: " + csvFile);
return;
}
BufferedReader br = new BufferedReader(new FileReader(csvFile));
String line = "";
String splitBy = ",";
while ((line = br.readLine()) != null) {
String[] fields = line.split(splitBy);
if (fields.length < 5)
continue;
String name = fields[0];
String sidc = fields[1];
String geometryType = fields[4];
MilitarySymbol newSymbol = new MilitarySymbol(name, sidc, geometryType);
if (newSymbol.isValidSymbol()) {
symbolList.add(newSymbol);
}
}
br.close();
for (MilitarySymbol s : symbolList )
{
String name = s.getName();
String symbolId = s.getSymbolId();
// IMPORTANT: note toUpperCase() - all keys will be upper case
if (!sidc2SymbolProps.containsKey(symbolId))
sidc2SymbolProps.put(symbolId.toUpperCase(), s);
if (!symbolName2SymbolProps.containsKey(name))
symbolName2SymbolProps.put(name.toUpperCase(), s);
// To see symbols list/table:
// System.out.println(name + ":" + symbolId + ":" + geoType);
}
System.out.println("Initialization complete: unique symbols found=" + sidc2SymbolProps.size());
} catch (IOException e) {
// Should not happen
e.printStackTrace();
}
}
public String symbolIdToName(String symbolId)
{
String symbolName = NOT_FOUND;
if ((symbolId == null) || symbolId.isEmpty())
return symbolName;
String maskedSymbolId = getMaskedSymbolId(symbolId);
if (!sidc2SymbolProps.containsKey(maskedSymbolId))
{
// try again with "F"/friendly version (some only have this version)
maskedSymbolId = getMaskedSymbolId(symbolId, "F");
if (!sidc2SymbolProps.containsKey(maskedSymbolId))
{
System.out.println(maskedSymbolId + "-not found in table, can't continue");
return symbolName;
}
}
MilitarySymbol sp = sidc2SymbolProps.get(maskedSymbolId);
if (sp == null) // should not happen
return symbolName;
symbolName = sp.getName();
return symbolName;
}
public String symbolNameToId(String symbolName)
{
String symbolId = NOT_FOUND;
if ((symbolName == null) || symbolName.isEmpty())
return symbolId;
String lookupSymbolName = symbolName.toUpperCase();
if (lookupSymbolName.contains("~"))
{
lookupSymbolName = lookupSymbolName.split("~")[0];
}
try
{
if (!symbolName2SymbolProps.containsKey(lookupSymbolName))
{
lookupSymbolName = lookupSymbolName + " F";
if (!symbolName2SymbolProps.containsKey(lookupSymbolName))
{
System.out.println(symbolName + "-not found in table, can't continue");
return symbolId;
}
}
MilitarySymbol sp = symbolName2SymbolProps.get(lookupSymbolName);
if (sp == null) // should not happen
return symbolId;
symbolId = sp.getSymbolId();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return symbolId;
}
private String getMaskedSymbolId(String symbolId, String affiliation)
{
if (symbolId.length() < 15)
return symbolId;
StringBuilder sb = new StringBuilder();
Character codingScheme = symbolId.charAt(0);
sb.append(codingScheme);
sb.append(affiliation);
sb.append(symbolId.charAt(2));
sb.append('P');
for (int i = 4; i<10; i++)
sb.append(symbolId.charAt(i));
for (int i = 10; i<14; i++)
sb.append('-');
if (codingScheme == 'G')
sb.append('X');
else
sb.append('-');
return sb.toString().toUpperCase();
}
private String getMaskedSymbolId(String symbolId)
{
if ((symbolId == null) || (symbolId.length() < 15))
return symbolId;
String affiliation = getAffiliationChar(symbolId);
return getMaskedSymbolId(symbolId, affiliation);
}
private String getAffiliationChar(String symbolId)
{
Character ch = symbolId.toUpperCase().charAt(1);
Character affilCh = ch;
switch (ch)
{
// Simple cases (no change of affil char)
case 'F':
case 'H':
case 'U':
case 'N':
affilCh = ch;
break;
// Friendlies
case 'M':
case 'A':
case 'D':
case 'J':
case 'K':
affilCh = 'F';
break;
// Hostile
case 'S':
affilCh = 'H';
break;
// Neutral
case 'L':
affilCh = 'N';
break;
// Unknown
case 'P':
case 'G':
case 'W':
case '-':
default :
affilCh = 'U';
break;
}
return affilCh.toString();
}
}
public class TestIt {
/**
* @param args
*/
public static void main(String[] args) {
SymbolLookup symbolLookup = new SymbolLookup();
System.out.println("testSymbolIdToName");
String sic2Check = "GHMPOGL-----USG";
String expectedName = "General Obstacle Line H";
String actualName = symbolLookup.symbolIdToName(sic2Check);
System.out.println("SIC: " + sic2Check + ", returned Name: " + actualName + " / Expected=" + expectedName);
System.out.println("testSymbolNameToId");
String name2Check = "Limited Access Area H";
String sidc = symbolLookup.symbolNameToId(name2Check);
String expectedSic = "GHGPGAY-------X";
System.out.println("Name: " + name2Check + ", returned SIC: " + sidc + " / Expected=" + expectedSic);
System.out.println("testSymbolNameToId3");
String name2Check3 = "Infantry";
String sidc3 = symbolLookup.symbolNameToId(name2Check3);
String expectedSic3 = "SFGPUCI--------";
System.out.println("Name: " + name2Check3 + ", returned SIC: " + sidc3 + " / Expected=" + expectedSic3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment