Calculating number of bands in a raster - raster

I have been trying to calculate the number of bands inside a raster file. While the below codes works mostly, but it seems to throw an exception when i tried the code on a projected raster.
I have tried the following code:
AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
// this is a bit hacky but does make more geotiffs work
Hints hints = new Hints();
if (format instanceof GeoTiffFormat) {
hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
}
reader = format.getReader(rasterFile, hints);
GridCoverage2D result = reader.read(null);
int bands = result.getNumSampleDimensions();
System.out.println(bands)
I am getting the following exception :
org.geotools.data.DataSourceException
at org.geotools.gce.geotiff.GeoTiffReader.<init>(GeoTiffReader.java:266)
at org.geotools.gce.geotiff.GeoTiffFormat.getReader(GeoTiffFormat.java:307)
at org.geotools.gce.geotiff.GeoTiffFormat.getReader(GeoTiffFormat.java:76)
at ImageLab.displayLayers(ImageLab.java:74)
at ImageLab.getLayersAndDisplay(ImageLab.java:36)
at ImageLab.main(ImageLab.java:25)
Caused by: org.geotools.data.DataSourceException
at org.geotools.gce.geotiff.GeoTiffReader.getHRInfo(GeoTiffReader.java:500)
at org.geotools.gce.geotiff.GeoTiffReader.<init>(GeoTiffReader.java:253)
... 5 more
Caused by: java.lang.NullPointerException
at org.geotools.referencing.cs.AbstractCS.axisUsingUnit(AbstractCS.java:503)
at org.geotools.referencing.cs.DefaultCartesianCS.usingUnit(DefaultCartesianCS.java:291)
at org.geotools.coverage.grid.io.imageio.geotiff.GeoTiffMetadata2CRSAdapter.createUserDefinedPCS(GeoTiffMetadata2CRSAdapter.java:750)
at org.geotools.coverage.grid.io.imageio.geotiff.GeoTiffMetadata2CRSAdapter.createProjectedCoordinateReferenceSystem(GeoTiffMetadata2CRSAdapter.java:245)
at org.geotools.coverage.grid.io.imageio.geotiff.GeoTiffMetadata2CRSAdapter.createCoordinateSystem(GeoTiffMetadata2CRSAdapter.java:181)
at org.geotools.gce.geotiff.GeoTiffReader.getHRInfo(GeoTiffReader.java:330)
... 6 more
Exception in thread "main" java.lang.NullPointerException
at ImageLab.displayLayers(ImageLab.java:75)
at ImageLab.getLayersAndDisplay(ImageLab.java:36)
at ImageLab.main(ImageLab.java:25)
For the projected data do ineed to add some code as well.My Raster File is in WyonmingNAD27 : 32056 projection system

I was missing the following dependency in my code.
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
After I added the above dependency, the problem with CRS got resolved.

Related

JanusGraph BulkLoad reports NoSuchElement Error, nodes loaded but no edges

I am exporing ways of loading csv in JanusGraph. I tried the grateful-dead example given by the official document and it worked just fine. Approach as follows:
hadoop-load-csv.properties
gremlin.graph=org.apache.tinkerpop.gremlin.hadoop.structure.HadoopGraph
gremlin.hadoop.graphReader=org.apache.tinkerpop.gremlin.hadoop.structure.io.script.ScriptInputFormat
gremlin.hadoop.scriptInputFormat.script=./data/script-input-grateful-dead.groovy
gremlin.hadoop.inputLocation=./data/grateful-dead.txt
gremlin.hadoop.graphWriter=org.apache.hadoop.mapreduce.lib.output.NullOutputFormat
gremlin.hadoop.graphOutputFormat=org.apache.hadoop.mapreduce.lib.output.NullOutputFormat
gremlin.hadoop.outputLocation=output
gremlin.hadoop.jarsInDistributedCache=true
janusgraph-grateful.properties
gremlin.graph=org.janusgraph.core.JanusGraphFactory
storage.hbase.table=grateful
storage.hostname=
run.groovy
outputGraphConfig = [path to janusgraph-grateful.properties]
:load ./data/grateful-dead-jansugraph-schema.groovy
graph = JanusGraphFactory.open(outputGraphConfig)
defineGratefulDeadSchema(graph)
graph.close()
readGraph = GraphFactory.open([path to hadoop-load-csv.properties])
blvp = BulkLoaderVertexProgram.build().writeGraph(outputGraphConfig).create(readGraph)
readGraph.compute(SparkGraphComputer).program(blvp).submit().get()
g = GraphFactory.open(outputGraphConfig).traversal()
g.V().count()
g.E().count()
After that I dropped the whole graph, subsampled the data and loaded it again, and it failed.
1,song,HEY BO DIDDLEY,cover,5 followedBy,2,1|followedBy,3,2|followedBy,4,1|followedBy,5,1|followedBy,6,1
2,song,IM A MAN,cover,1 followedBy,1,1
3,song,NOT FADE AWAY,cover,531 followedBy,5,572 followedBy,5,40|followedBy,1,2
4,song,BERTHA,original,394 followedBy,10,4 followedBy,1,1
5,song,GOING DOWN THE ROAD FEELING BAD,cover,293
6,song,MONA,cover,1 sungBy,3|writtenBy,5 followedBy,1,1|followedBy,2,1
7,song,WHERE HAVE THE HEROES GONE,,0 followedBy,8,1 followedBy,9,1
8,song,OH BOY,cover,2 followedBy,9,1|followedBy,3,1|followedBy,7,1|sungBy,5|writtenBy,4 followedBy,1,1|followedBy,7,1|followedBy,6,1
800,song,WINING BOY BLUES,cover,1 sungBy,5|writtenBy,4
9,song,HERE COMES SUNSHINE,original,65 followedBy,10,1 followedBy,6,2
10,song,HERE COMES SUNSHINE,original,65
I got a NoSuchElement Error and when I looked into the graph, g.V().count() returns 10 while g.E().count() returns 0.
Does anyone know what is happening? It would be very kind of you to give me some advice.

I am trying to extract values and place them in a .db to use later, however my code has bugged and I can no longer even load SQLite

Using :
-IronPython
-AutoDesk
-Revit (PyRevit)
-Revit API
-SQLite3
My code is as follows:
try:
conn = sqlite3.connect('SQLite_Python.db')
c = conn.cursor()
print("connected")
Insert_Volume = """INSERT INTO Column_Coordinates
(x, y, z)
VALUES
(1, 2, 3)"""
count = c.execute(Insert_Volume)
conn.commit()
print("Volume values inserted", c.rowcount)
c.close()
except sqlite3.Error as error:
print("Failed to insert data into sqlite table", error)
finally:
if (conn):
conn.close()
print("The SQLite connection is closed")'''
This code used to work within PyRevit, but now does not, with the following error:
Exception : System.IO.IOException: Could not add reference to assembly IronPython.SQLite
Please advise, this is one of the early steps of a large project and therefore is delaying my work quite a bit.
I look forward to your reply.

Why am I forced to put a first line commentary in validators.en.yml?

I have an entity with the following annotation
#Assert\Callback(methods={"checkValidity"})
And the method, under a condition adds an error :
public function checkValidity(ExecutionContextInterface $context)
{
if ($this->getAmount() < $this->getMinimumAmount()) {
$context->buildViolation('app.forms.transfer.errors.amountLowerThanMinimum')
->atPath('amount')
->setParameter('%minimumAmount%', $this->getMinimumAmount())
->addViolation();
}
}
To make it work, I have to put theses lines in validator.en.yml
# line: foo
app:
forms:
transfer:
errors:
balanceTooLow: Your balance is too low to transfer %amount% to %application%
amountLowerThanMinimum: The application %application% required that you transfer a minimum of $%minimumAmount%
It works perfectly, but if I remove this first dummy line
# line: foo
It doesn't translate my error anymore.
After searching for an explanation during an entire hour, I still don't understand what could explain this strange behaviour. Any idea ?

How to mock 2 or multiple prepared statements per connection in jMock?

public void testCreate() throws ApplicationException {
DutyDrawback drawback = new DutyDrawback();
drawback.setSerialNumber("TEST123");
drawback.setSnProcessInd("Y");
drawback.setMediaNumber("TEST111");
drawback.setMnProcessInd("Y");
drawback.setConfirmedInd("Y");
drawback.setResponseCode("1");
drawback.setResponseMsg("MSG");
drawback.setLastChangedUser("USER");
drawback.setLastChangedDate(new Date(System.currentTimeMillis()));
mockILogger.expects(atLeastOnce()).method("informationalEvent");
Logger.setMockLogger((ILogger) mockILogger.proxy());
// Set up the statement expectations
Mock mockStatement = mock(PreparedStatement.class);
mockStatement.expects(atLeastOnce()).method("setString");
mockStatement.expects(once()).method("executeUpdate").will(returnValue(1));
Mock mockStatement1 = mock(PreparedStatement.class);
mockStatement1.expects(atLeastOnce()).method("setString");
mockStatement1.expects(once()).method("executeUpdate").will(returnValue(1));
// Set up the connection expectations
mockConnection.expects(once()).method("setAutoCommit");
mockConnection.expects(once()).method("commit");
mockConnection.expects(once()).method("prepareStatement").will(returnValue(mockStatement1.proxy()));
mockConnection.expects(once()).method("prepareStatement").will(returnValue(mockStatement.proxy()));
TVSUtils.setMockConnection((Connection) mockConnection.proxy());
DutyDrawbackDAO drawbackDAO = new DutyDrawbackDAO();
int count = drawbackDAO.create(drawback);
assertEquals(2, count);
}
I am getting the following trace:
org.jmock.core.DynamicMockError: mockPreparedStatement: unexpected invocation
Invoked: mockPreparedStatement.executeUpdate()
Allowed:
expected at least once and has been invoked: setString, is void
expected once and has been invoked: executeUpdate, returns <1>
at org.jmock.core.AbstractDynamicMock.mockInvocation(AbstractDynamicMock.java:96)
at org.jmock.core.CoreMock.invoke(CoreMock.java:39)
at $Proxy2.executeUpdate(Unknown Source)
at com.cat.tvs.dao.DutyDrawbackDAO.create(DutyDrawbackDAO.java:102)
at com.cat.tvs.dao.DutyDrawbackDAOTest.testCreate(DutyDrawbackDAOTest.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:58)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:60)
at java.lang.reflect.Method.invoke(Method.java:391)
at junit.framework.TestCase.runTest(TestCase.java:164)
at org.jmock.core.VerifyingTestCase.runBare(VerifyingTestCase.java:39)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
I am not sure what am I doing wrong here?
Thanks.
Firstly, you seem to be using jmock1, you might consider moving to jmock2.
The failure says that you're calling executeUpdate() when it's not expected. Are you calling it more than once?
Finally, I usually recommend not writing mock tests against JDBC, even though I wrote a paper many years ago showing how to do it. We talk about "only mock types you own" for a number of reasons, one of which is that mock tests assume that the third party implementation doesn't change. For JDBC, I'd recommend writing integration tests that run against a small instance of the real database.

Collapse a jung node based on node selection

I am trying to collapse all the children of a vertex when a user clicks on it. But the problem is that, every time I tried to do so, I got the following error:
Tree must not contain edu.ics.jung.graph.DelegateForest#17510d96
The code is given below:
public void graphClicked(MyNode v, MouseEvent me)
{
Collection<MyNode> childrens = graph.getChildren(v);
Collection picked = new Hashset(childrens);
if(picked.size>1)
{
Graph ingraph = this.radialLayout.getGraph();
Graph clusterGraph = collapser.getClusterGraph(graph,childrens);
Graph g = collapser.collapse(ingraph,clustergraph); //The error report points on this line
.
.
.
}
}
I am using a Forest with RadialLayout.
Can anyone help me? How can I solve the problem?
Check out the tree node collapse and vertex collapse demos here:
http://jung.sourceforge.net/applet/index.html
The source for each of these is included in the distribution files.

Resources