Gianpaolo Coro 2014-03-03 09:46:36 +00:00
parent c49d678970
commit 06b2912a76
2 changed files with 282 additions and 0 deletions

View File

@ -0,0 +1,145 @@
package org.gcube.dataanalysis.geo.utils;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class GdalConverter {
public static void main1(String[] args){
// -projwin -10 10 10 -10
System.out.println(convertToGeoTiff("p_edulis_map.img"));
}
public static void main(String[] args){
System.out.println(convertToGeoTiff(args[0]));
}
static String gdalExecutorWin = "C:/Program Files (x86)/GDAL/gdal_translate";
static String gdalExecutorLin = "/usr/bin/gdal_translate";
public static String convertToGeoTiff(String fullPathToFile){
String gdalConverter = "";
if (fullPathToFile.endsWith("tiff"))
return fullPathToFile;
if (System.getProperty("os.name").contains("Win"))
gdalConverter = gdalExecutorWin;
else
gdalConverter = gdalExecutorLin;
System.out.println("Executing transformation in "+System.getProperty("os.name")+"->"+gdalConverter);
int pointIndex = fullPathToFile.lastIndexOf(".");
if (pointIndex<0)
pointIndex = fullPathToFile.length();
String geoTiffFile = fullPathToFile.substring(0,pointIndex)+".tiff";
String executionResult = ExecuteGetLine(gdalConverter+" -of GTiff "+fullPathToFile+" "+geoTiffFile);
if (executionResult.equalsIgnoreCase("error"))
return null;
else
return geoTiffFile;
}
public static String convertToASC(String fullPathToFile){
String gdalConverter = "";
if (System.getProperty("os.name").contains("Win"))
gdalConverter = gdalExecutorWin;
else
gdalConverter = gdalExecutorLin;
System.out.println("Executing transformation in "+System.getProperty("os.name")+"->"+gdalConverter);
int pointIndex = fullPathToFile.lastIndexOf(".");
if (pointIndex<0)
pointIndex = fullPathToFile.length();
String ascTiffFile = fullPathToFile.substring(0,pointIndex)+".asc";
String executionResult = ExecuteGetLine(gdalConverter+" -of AAIGrid "+fullPathToFile+" "+ascTiffFile);
if (executionResult.equalsIgnoreCase("error"))
return null;
else
return ascTiffFile;
}
public static String ExecuteGetLine(String cmd){
Process process = null;
String lastline = "";
try {
System.out.println("OSCommand-> Executing Control ->"+cmd);
process = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = br.readLine();
System.out.println("OSCommand-> line->"+line);
while (line!=null){
try{
lastline = line;
System.out.println("OSCommand-> line->"+line);
line = br.readLine();
}catch(EOFException e){
System.out.println("OSCommand -> Process Finished with EOF");
break;
}
catch(Exception e){
line = "ERROR";
break;
}
}
System.out.println("OSCommand -> Process Finished");
} catch (Throwable e) {
System.out.println("OSCommand-> error ");
e.printStackTrace();
lastline = "ERROR";
}
process.destroy();
System.out.println("OSCommand-> Process destroyed ");
return lastline;
}
public static boolean FileCopy (String origin,String destination){
try{
File inputFile = new File(origin);
System.out.println("OSCommand-> FileCopy-> "+inputFile.length()+" to "+inputFile.canRead());
int counterrors=0;
while ((inputFile.length()==0)&&(counterrors<10)){
Thread.sleep(20);
counterrors++;
}
File outputFile = new File(destination);
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}

View File

@ -0,0 +1,137 @@
package org.gcube.dataanalysis.geo.utils;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageInputStream;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
public class GeoTiffMetadata {
public static void main(String[] args) throws Exception{
String[] argss={"C:/Users/coro/Dropbox/Public/geoserver-GetCoverage.image.geotiff"};
GeoTiffMetadata meta = new GeoTiffMetadata();
int length = argss.length;
for ( int i = 0; i < length; i++ )
meta.readAndDisplayMetadata( argss[i] );
}
public void readAndDisplayMetadata( String fileName ) {
ImageInputStream iis =null;
try {
File file = new File( fileName );
iis = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {
// pick the first available ImageReader
ImageReader reader = readers.next();
// attach source to the reader
reader.setInput(iis, true);
// read metadata of first image
IIOMetadata metadata = reader.getImageMetadata(0);
String[] names = metadata.getMetadataFormatNames();
int length = names.length;
for (int i = 0; i < length; i++) {
AnalysisLogger.getLogger().debug("Format name: " + names[ i ] );
displayMetadata(metadata.getAsTree(names[i]));
}
}
AnalysisLogger.getLogger().debug("scalex "+xScale);
AnalysisLogger.getLogger().debug("scaley "+yScale);
AnalysisLogger.getLogger().debug("scalez "+zScale);
}
catch (Exception e) {
e.printStackTrace();
}finally{
if (iis!=null)
try {
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
void displayMetadata(Node root) {
displayMetadata(root, 0);
}
void indent(int level) {
for (int i = 0; i < level; i++)
System.out.print(" ");
}
boolean capturexyz=false;
public double xScale=-1;
public double yScale=-1;
public double zScale=-1;
void displayMetadata(Node node, int level) {
// print open tag of element
indent(level);
System.out.print("<" + node.getNodeName());
NamedNodeMap map = node.getAttributes();
if (map != null) {
// print attribute values
int length = map.getLength();
for (int i = 0; i < length; i++) {
Node attr = map.item(i);
String name = attr.getNodeName();
String value = attr.getNodeValue();
if (name.equalsIgnoreCase("name")&&value.equalsIgnoreCase("ModelPixelScaleTag"))
capturexyz=true;
else if (name.equalsIgnoreCase("name"))
capturexyz=false;
if (capturexyz && name.equalsIgnoreCase("value")){
if (xScale<0)
xScale=Double.parseDouble(value);
else if (yScale<0)
yScale=Double.parseDouble(value);
else if (zScale<0)
zScale=Double.parseDouble(value);
}
System.out.print(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() + "\"");
}
}
Node child = node.getFirstChild();
if (child == null) {
// no children, so close element and return
AnalysisLogger.getLogger().debug("/>");
return;
}
// children, so close current tag
AnalysisLogger.getLogger().debug(">");
while (child != null) {
// print children recursively
displayMetadata(child, level + 1);
child = child.getNextSibling();
}
// print close tag of element
indent(level);
AnalysisLogger.getLogger().debug("</" + node.getNodeName() + ">");
}
}