1

I am trying to integrate units and quantity with JSR-363. I know and understand how to create SI and NONSI units and convert e.g. Areas units from one to another.

But, in a kind of geographic cartographic way, I want to create another unit: from absolute population number to a relative on e.g. population per km²

First question: In what unit can I put numbers with plane quantities numbers like population number or amount of eggs.

Second question: How can I create a new unit with combination with Area and amount number without any java.lang.ClassCastException

I am using maven packages and Java 8

</dependency>
  <dependency>
      <groupId>javax.measure</groupId>
      <artifactId>unit-api</artifactId>
      <version>1.0</version>
  </dependency>
  <dependency>
      <groupId>tec.units</groupId>
      <artifactId>unit-ri</artifactId>
      <version>1.0.3</version>
  </dependency>
<dependency>
  <groupId>si.uom</groupId>
  <artifactId>si-units-java8</artifactId>
  <version>0.9</version>
</dependency>

and the example

 Quantity<Area> area = Quantities.getQuantity(4000.0,SI.SQUARE_METRE);
 Quantity<*????*> population = Quantities.getQuantity(60, *????*);

 Quantity< ???> result= population.divide(area).getValue();

The goal is to use any number for population and any number of area, where it is also possible to switch between m² or km² or any other area unit

What I also don't really understand is, how to get units and values from a calculation in a new Quantity class ala result?

==== UPDATE

with the code example and BaseUnit I can create the result.

but with my maven packages I get an Exception

 Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Unit
at tec.units.ri.format.SimpleUnitFormat$DefaultFormat.format(SimpleUnitFormat.java:692)
at tec.units.ri.format.QuantityFormat$Standard.format(QuantityFormat.java:188)
at tec.units.ri.format.QuantityFormat.format(QuantityFormat.java:125)
at tec.units.ri.format.QuantityFormat.format(QuantityFormat.java:143)
at tec.units.ri.quantity.DoubleQuantity.toString(DoubleQuantity.java:121)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)

it is working, if I use the getValue or getUnit method. But this way is less

import si.uom.SI;
import tec.units.ri.quantity.Quantities;
import tec.units.ri.unit.BaseUnit;

import javax.measure.Quantity;
import javax.measure.quantity.Area;
import javax.measure.quantity.Dimensionless;

public class App{
    public static void main( String[] args ){

    BaseUnit<Dimensionless> PEOPLE = new BaseUnit<>("people");

    Quantity<Area> area = Quantities.getQuantity(4000.0, SI.SQUARE_METRE);

    Quantity<Dimensionless> pop = Quantities.getQuantity(1000, PEOPLE);

    System.out.println(area); //not working with exception
    System.out.println(pop); //not working with exception
    System.out.println(pop.divide(area));//not working with exception


    System.out.println(pop.divide(area).getValue());
    System.out.println(pop.divide(area).getUnit());




  }
}

closed as unclear what you're asking by Jochen Schwarze, Vince, Dan C, Erik, whyzar Mar 4 at 14:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I seem to be using slightly different imports to you, see my update – Ian Turton Mar 4 at 8:38
1

From a quick test it seems like you can create a new BaseUnit people and use it as you'd expect.

import si.uom.NonSI;
import si.uom.SI;
import systems.uom.common.USCustomary;
import tec.uom.se.quantity.Quantities;
import tec.uom.se.unit.BaseUnit;
import tec.uom.se.unit.MetricPrefix;

...

BaseUnit<Dimensionless> PEOPLE = new BaseUnit<>("people");
javax.measure.Quantity<Dimensionless> pop = Quantities.getQuantity(10000, PEOPLE);

javax.measure.Quantity<Area> a = me.calcArea(feature);
System.out.println(a);
System.out.println(pop);
System.out.println(pop.divide(a));
@SuppressWarnings("unchecked")
Unit<Area> sq_km = (Unit<Area>) MetricPrefix.KILO(SI.METRE).multiply(MetricPrefix.KILO(SI.METRE));
System.out.println(a.to(sq_km));
System.out.println(pop.divide(a.to(sq_km)));

gives me the expected:

469013.24064056645 m²
10000 people
0.02132135968345425028597612117922469 people/m²
0.46901324064056643 km²
21321.35968345425119517687868168138 people/km²
0.1810870245963103 mi²
46.901324064056645 ha
115.89569574163858 acres

I'm using GeoTools as my base so I'm depending on

<dependency>
  <groupId>systems.uom</groupId>
  <artifactId>systems-common-java8</artifactId>
  <version>0.7.2</version>
</dependency>

which pulls in uom-se 1.0.8.

  • ah thanks. Quantity<Dimensionless> was the mising info for me. unfortunatelly i get an exceptions during print Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Unit – F.M. Mar 1 at 15:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.