Skip to content

Commit

Permalink
Full restructuring of demat to use vega-lite spec directly with JAVET…
Browse files Browse the repository at this point in the history
… instead of maintaining a pojo

All vega-lite examples work except image loading, which is not supported.
  • Loading branch information
ebocher committed Apr 16, 2024
1 parent 0c354fc commit e90452e
Show file tree
Hide file tree
Showing 18 changed files with 1,109 additions and 428 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.orbisgis</groupId>
<artifactId>demat</artifactId>
<version>0.0.8-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Demat is breton word to said "Hello". Demat is wrapper on top of the Vega-lite library</description>
<description>Demat is breton word to said "Hello". Demat is library to run Vega-lite specifications.</description>
<!-- Properties -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
26 changes: 15 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@ Demat is a French Brittany word to said Hello (http://devri.bzh/dictionnaire/d/d

Demat is a java library to generate [Vega-lite](https://vega.github.io/vega-lite/) representations.


Demat is under active development, nothing is stable ;-)
Demat uses the JAVET library to execute JS code.


# Use it

Let's run this script with Groovy
Let's run this script with Groovy to draw a simple bar chart and save it in a svg file.

```groovy
@GrabResolver(name = 'orbisgis', root = 'https://oss.sonatype.org/content/repositories/snapshots/')
@Grab(group = 'org.orbisgis', module = 'demat', version = '0.0.7-SNAPSHOT')
@Grab(group = 'org.orbisgis', module = 'demat', version = '1.0.0-SNAPSHOT')
import static org.orbisgis.demat.Demat.*
def chart = Chart().description("A simple bar chart").name("A simple name")
.data(new Object[][]{{"a", "b", "c"}, {1, 22, 12}, {200, 300, 400}})
.mark(Bar())
.encode(X("a").nominal(), Y("b").quantitative())
chart.show()
def spec = [
description:"A simple bar chart with embedded data.",
data: [values: [
["a": "A", "b": 28], ["a": "B", "b": 55], ["a": "C", "b": 43],
["a": "D", "b": 91], ["a": "E", "b": 81], ["a": "F", "b": 53],
["a": "G", "b": 19], ["a": "H", "b": 87], ["a": "I", "b": 52]]
],
mark:"bar",
encoding : [x : [field:"a",axis:[LabelAngle:0],type:"nominal"],
y: [field:"b","type":"quantitative"]]]
toSVG(spec, "/tmp/bar_chart.svg")
```

shows https://vega.github.io/vega-lite/examples/bar.html

shows https://vega.github.io/vega-lite/examples/bar.html
217 changes: 217 additions & 0 deletions src/main/java/org/orbisgis/demat/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,56 @@
/*
* DEMAT is java wrapper on top of the vega-lite library
*
* Demat is breton word to said "Hello".
*
* DEMAT is part of the OrbisGIS platform.
*
* OrbisGIS platform is a set of open source tools to access, process, display
* and share geographical informations.
*
* It is leaded by CNRS within the French Lab-STICC laboratory <http://www.lab-sticc.fr/>,
* DECIDE team of Vannes.
*
* OrbisGIS is dedicated to research in GIScience.
*
* The GIS group of the DECIDE team is located at :
*
* Laboratoire Lab-STICC – CNRS UMR 6285
* Equipe DECIDE
* UNIVERSITÉ DE BRETAGNE-SUD
* Institut Universitaire de Technologie de Vannes
* 8, Rue Montaigne - BP 561 56017 Vannes Cedex
*
* DEMAT is distributed under LGPL 3 license.
*
* Copyright (C) 2021 CNRS (Lab-STICC UMR CNRS 6285)
*
*
* DEMAT is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* DEMAT is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* DEMAT. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, please consult: <http://www.orbisgis.org/>
* or contact directly:
* info_at_ orbisgis.org
*/
package org.orbisgis.demat;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUtils {

Expand Down Expand Up @@ -90,4 +140,171 @@ public static boolean isExtensionWellFormated(File file, String... prefixes) {
}
return false;
}

/**
*
* @param reader
* @return
* @throws IOException
*/
public static Object json(InputStream reader) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.readValue(reader, Object.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/**
*
* @param jsonFile
* @return
* @throws IOException
*/
public static Object json(File jsonFile) throws IOException {
if(FileUtils.isExtensionWellFormated(jsonFile, "json", "geojson")) {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonFile, Object.class);
}
throw new RuntimeException("Cannot read as json file.");
}

/**
*
* @param jsonFile
* @return
* @throws IOException
*/
public static Object geojson(File jsonFile) throws IOException {
if(FileUtils.isExtensionWellFormated(jsonFile, "json", "geojson")) {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(jsonFile, Object.class);
}
throw new RuntimeException("Cannot read as json file.");
}


/**
* Read a CSV {@link File} and convert it to a Data object
* @param csvFile
* @return
* @throws IOException
*/
public static List csv(File csvFile) throws IOException {
if(FileUtils.isExtensionWellFormated(csvFile, "csv")) {
String line = "";
//use comma as separator
String splitBy = ",";

ArrayList lines = new ArrayList();
try (BufferedReader br =
new BufferedReader(new FileReader(csvFile))) {

//Header
line = br.readLine();
String[] header = null;
if (line != null) {
header = line.split(splitBy);
}
while ((line = br.readLine()) != null) {
Map<String, Object> values = new HashMap<>();
String[] csvValues = line.split(splitBy);
for (int i = 0; i < header.length; i++) {
values.put(header[i], csvValues[i]);
}
lines.add(values);
}
}

return lines;
}
return null;
}
/**
* Read a CSV {@link File} and convert it to a Data object
* @param tsvFile
* @return
* @throws IOException
*/
public static List tsv(File tsvFile) throws IOException {
if(FileUtils.isExtensionWellFormated(tsvFile, "tsv")) {
String line = "";
//use tabular as separator
String splitBy = "\t";
ArrayList lines = new ArrayList();
try (BufferedReader br =
new BufferedReader(new FileReader(tsvFile))) {
//Header
line = br.readLine();
String[] header = null;
if (line != null) {
header = line.split(splitBy);
}
while ((line = br.readLine()) != null) {
Map<String, Object> values = new HashMap<>();
String[] csvValues = line.split(splitBy);
for (int i = 0; i < header.length; i++) {
values.put(header[i], csvValues[i]);
}
lines.add(values);
}
}
return lines;
}
return null;
}

/**
* Method to open a browser
* Used by ImageJ
*
* @param url
* @throws Exception
*/
public static void openBrowser(String url) throws Exception {
if (url == null || url.isEmpty()) {
throw new RuntimeException("The URL cannot be null or empty");
}
String osname = System.getProperty("os.name");
boolean isWin = osname.startsWith("windows");
boolean isMac = !isWin && osname.startsWith("mac");
if (isMac) {
Runtime.getRuntime().exec("open " + url);
} else if (isWin) {
String cmd = "rundll32 url.dll,FileProtocolHandler " + url;
if (osname.startsWith("Windows 2000"))
cmd = "rundll32 shell32.dll,ShellExec_RunDLL " + url;
Process process = Runtime.getRuntime().exec(cmd);
// This avoids a memory leak on some versions of Java on Windows.
// That's hinted at in <http://developer.java.sun.com/developer/qow/archive/68/>.
try {
process.waitFor();
process.exitValue();
} catch (InterruptedException ie) {
throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
}
} else {
// Assume Linux or Unix
// Based on BareBonesBrowserLaunch (http://www.centerkey.com/java/browser/)
// The utility 'xdg-open' launches the URL in the user's preferred browser,
// therefore we try to use it first, before trying to discover other browsers.
String[] browsers = {"xdg-open", "netscape", "firefox", "konqueror", "mozilla", "opera", "epiphany", "lynx"};
String browserName = null;
try {
for (int count = 0; count < browsers.length && browserName == null; count++) {
String[] c = new String[]{"which", browsers[count]};
if (Runtime.getRuntime().exec(c).waitFor() == 0)
browserName = browsers[count];
}
if (browserName == null) {
throw new RuntimeException("Could not find a browser");
} else {
Runtime.getRuntime().exec(new String[]{browserName, url});
}
} catch (Exception e) {
throw new IOException("Exception while launching browser: " + e.getMessage());
}
}
}
}
Loading

0 comments on commit e90452e

Please sign in to comment.