Missing pixels when extracting each pixel value - raster

enter image description hereWe are trying to convert lzw compressed raster(tiff) into text format(meaning extracting each pixel centroid and band1 value.. We see that some parts of the raster is missed while conversion.. however there is a pattern in miss data (data is coming in unequal striped pattern) Please see snapshot up .. Yellow dots shows the centroid converted while no overlap with yellow the data is missed while converting..
we have tried the tile size 128 and 256 but the output was different in both the cases
List<String> tiffExtensions = new ArrayList<>();
tiffExtensions.add(".tif");
tiffExtensions.add(".TIF");
tiffExtensions.add(".tiff");
tiffExtensions.add(".TIFF");
final scala.Option<CRS> crsNone = scala.Option.apply(null);
final scala.Option<Object> objectNone = scala.Option.apply(null);
final scala.Option<Object> numPartitionsObject = scala.Option.apply(new Integer(10));
final scala.Option<Object> tileSize = scala.Option.apply(Integer.parseInt("256"));
final scala.Option<Object> partitionBytes = scala.Option.apply(128l * 1024 * 1024);
HadoopGeoTiffRDD.Options options = new HadoopGeoTiffRDD.Options$().apply(
JavaConverters.asScalaIteratorConverter(tiffExtensions.iterator()).asScala().toSeq(), crsNone,
HadoopGeoTiffRDD.GEOTIFF_TIME_TAG_DEFAULT(), HadoopGeoTiffRDD.GEOTIFF_TIME_FORMAT_DEFAULT(), tileSize,
numPartitionsObject, partitionBytes, objectNone);
RDD<Tuple2<ProjectedExtent, Tile>> rasterImageRdd = HadoopGeoTiffRDD.spatial(new Path(rastedImageDir), options,
javaSparkContext.sc());
JavaRDD<Tuple2<ProjectedExtent, Tile>> rasterImageJavaRdd = rasterImageRdd.toJavaRDD();
JavaRDD<String> pixelRdd = rasterImageJavaRdd
.flatMap(new FlatMapFunction<Tuple2<ProjectedExtent, Tile>, String>() {
private static final long serialVersionUID = -6395159549445351446L;
public Iterator<String> call(Tuple2<ProjectedExtent, Tile> v1) throws Exception {
ArrayList<String> list = new ArrayList<String>();
Tile t = v1._2;
ProjectedExtent projectedExtent = v1._1;
ProjectedRaster<CellGrid> r = new ProjectedRaster<CellGrid>(
new Raster<CellGrid>(t, projectedExtent.extent()), projectedExtent.crs());
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4283);
WKTWriter wktWriter = new WKTWriter();
for (int i = 0; i < t.rows(); i++) {
for (int j = 0; j < t.cols(); j++) {
StringBuilder sb = new StringBuilder();
if (!Double.isNaN(t.getDouble(j, i))) {
Double elevation = t.getDouble(j, i);
Tuple2<Object, Object> longLatTupel = r.raster().rasterExtent().gridToMap(j, i);
if (longLatTupel._2() != null && longLatTupel._1() != null) {
Double latitude = Double.parseDouble(longLatTupel._2() + "");
Double longitude = Double.parseDouble(longLatTupel._1() + "");
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
sb.append(elevation).append(TAB_SEP);
sb.append(point);
list.add(sb.toString());
}
}
}
}
return list.iterator();
}
});
_

Related

How to get inner ring(hole) from shapefile in ASP.NET using dotspatial?

I'm searching a script to get inner ring from a polygon in the shapefile using dotspatial. Below is my script to get all coordinate(ignoring outer/inner ring) from a shapefile.
string shapeFilePath = #"\example.shp";
shapeFilePath = location + shapeFilePath;
Shapefile indexMapFile = Shapefile.OpenFile(shapeFilePath);
indexMapFile.Reproject(KnownCoordinateSystems.Geographic.Australia.GeocentricDatumofAustralia1994);
for (int i = 0; i < indexMapFile.DataTable.Rows.Count; i++)
{
IFeature feature = indexMapFile.Features.ElementAt(i);
var arr = feature.Coordinates.ToArray();
foreach (var det in arr)
{
DotSpatial.Topology.Coordinate det_cor = det;
string X = det_cor.X.ToString();
string Y = det_cor.Y.ToString();
}
}
i found the answer, this is my code, how to get inner ring from polygon
string shapeFilePath = #"\example.shp";
shapeFilePath = location + shapeFilePath;
Shapefile indexMapFile = Shapefile.OpenFile(shapeFilePath);
indexMapFile.Reproject(KnownCoordinateSystems.Geographic.Australia.GeocentricDatumofAustralia1994);
for (int i = 0; i < indexMapFile.DataTable.Rows.Count; i++)
{
IFeature feature = indexMapFile.Features.ElementAt(i);
IPolygon bp = feature.GetBasicGeometryN(0) as IPolygon;
var all_polygon = bp.GetGeometryN(0);//include outer and inner ring, see this link -> https://stackoverflow.com/questions/8139739/wkt-how-do-you-define-polygons-with-3-rings-2-holes
var outer_ring = bp.Shell.GetGeometryN(0);
int idx_hole = 0;
foreach (var hole in bp.Holes)
{
//get inner ring/hole from polygon
Response.Write(bp.GetInteriorRingN(idx_hole));
idx_hole++;
}
}

How convert a raw image stored in a byte array to a rgb image with opencv and Java

i am working in the preview of a fingerprint scaner using id3Fingerprint sdk and OpenCV. If i just show the preview from the id3fingerprint sdk all is fine, but if i load it to a Mat object of OpenCV in order to draw some rectangles in the image then:
1.- The fingerprints are displayed in right form but the rectangles are displayed as lines or pixels in random x,y location.
2.- The rectangles are displayed in right form but the fingerprints are displayed "blured" (look the image attached).fingerprints are blured
I think, my problem is when i convert the raw grayscale image (a byte array from the id3fingerprint sdk) to a RGB or RGBA image.
private void showPreview2(FingerImage image){
int height = 750;
int width = 750;
int currentWidth = 0;
int currentHeight = 0;
try {
currentWidth = image.getWidth();
currentHeight = image.getHeight();
} catch (FingerException ex) {
Logger.getLogger(CallingID3Example.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] pixels = image.getPixels();
Mat dest = new Mat();
Mat source = new Mat();
Mat source2 = null;
source2 = new Mat(currentWidth, currentHeight, CvType.CV_8UC1);
source2.put(0, 0, pixels);
MatOfByte pix = new MatOfByte();
Imgcodecs.imencode(".bmp", source2, pix);
source2.put(0, 0, pix.toArray());
Imgproc.cvtColor(source2, source, Imgproc.COLOR_GRAY2RGBA);
try {
int i=0;
for(FingerImage finger : image.getSegments()){
Scalar color;
color = new Scalar(0, 250,0);
FingerBounds bound = image.getSegmentBounds()[i];
Imgproc.rectangle(source, new Point(bound.topLeft.x, bound.topLeft.y), new Point(bound.bottomRight.x, bound.bottomRight.y), color, 3);
double[] pixelTest;
pixelTest = source.get(bound.topLeft.x, bound.topLeft.y);
i++;
}
} catch (FingerException ex) {
Logger.getLogger(CallingID3Example.class.getName()).log(Level.SEVERE, null, ex);
}
gc = canvas.getGraphicsContext2D();
WritableImage writableImage = loadImage(source);
imageView.setImage(writableImage);
}
private WritableImage loadImage(Mat matrix) {
// Encoding the image
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".bmp", matrix, matOfByte);
// Storing the encoded Mat in a byte array
byte[] byteArray = matOfByte.toArray();
// Displaying the image
InputStream in = new ByteArrayInputStream(byteArray);
BufferedImage bufImage = null;
try {
bufImage = ImageIO.read(in);
} catch (IOException ex) {
}
// Creating the Writable Image
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
return writableImage;
}
Thanks for your answer.
You could try something like this:
// You need to know width/height of the image
int width = 0;
int height = 0;
byte[] imageSrc = null;//
// Convert 8bit greyscale byte array to RGBA byte array.
byte[] imageRGBA = new byte[imageSrc.length * 4];
int i;
for (i = 0; i < imageSrc.length; i++) {
imageRGBA[i * 4] = imageRGBA[i * 4 + 1] = imageRGBA[i * 4 + 2] = ((byte) ~imageSrc[i]);
// Invert the source bits
imageRGBA[i * 4 + 3] = -1;// 0xff, that's the alpha.
}
// Convert RGBA byte array to PNG
int samplesPerPixel = 4;
int[] bandOffsets = {0,1,2,3}; // RGBA order
byte[] bgraPixelData = new byte[width * height * samplesPerPixel];
DataBuffer buffer = new DataBufferByte(bgraPixelData, bgraPixelData.length);
WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, samplesPerPixel * width, samplesPerPixel, bandOffsets, null);
ColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
System.out.println("image: " + image); // Should print: image: BufferedImage#<hash>: type = 0 ...
ImageIO.write(image, "PNG", new File(path));
Update
To draw rectangle on image:
BufferedImage image = ...
Graphics2D graph = img.createGraphics();
graph.setColor(Color.BLACK);
graph.fill(new Rectangle(x, y, width, height));
graph.dispose();
ImageIO.write(image, "PNG", new File(path));

Make chips Selectable on mouse click event in wafermap jfree chart (javafx application)

I have created wafermap chart. I want to make chips(die) in wafer selectable on mouse click and insert labels as well lines from one chip to another. Anyone expert in jfree chart?
wafermap chart
Here are the basic pieces for a tooltip listener for wafer maps, which is a form of selecting the die. Add the following to WaferMapPlot:
public String findChipAtPoint(double x, double y, Rectangle2D plotArea){
double[] xValues = this.getChipXValues(plotArea, dataset.getMaxChipX()
+ 2, dataset.getChipSpace());
double startX = xValues[1];
double chipWidth = xValues[0];
int ychips = this.dataset.getMaxChipY()+ 2;
double[] yValues = this.getChipYValues(plotArea, ychips,
dataset.getChipSpace());
double startY = yValues[1];
double chipHeight = yValues[0];
double chipSpace = dataset.getChipSpace();
int chipX = (int)Math.floor((x - startX + chipWidth + chipSpace) /
(chipWidth + chipSpace));
int chipY = (int)Math.floor((y - startY + chipHeight + chipSpace) /
(chipHeight + chipSpace));
chipX = chipX - dataset.getXOffset() - 1;
chipY = ychips - chipY - dataset.getYOffset() - 1;
StringBuilder sb = new StringBuilder("(");
Number value = dataset.getChipValue(chipX, chipY);
if (value instanceof Double)
value = value.intValue();
sb.append(chipX).append(",").append(chipY).append(") ").append(
(value == null) ? "" : value.toString());
return sb.toString();
}
Then make a subclass of ChartPanel that will be the listener:
public class WaferMapChartPanel extends ChartPanel {
WaferMapPlot waferPlot = null;
WaferMapDataset dataSet = null;
public WaferMapChartPanel(JFreeChart chart){
super(chart);
waferPlot = (WaferMapPlot)chart.getPlot();
if (waferPlot != null)
dataSet = waferPlot.getDataset();
}
/**
* Returns a string for the tooltip.
* #param e the mouse event.
* #return A tool tip or <code>null</code> if no tooltip is available.
*/
#Override
public String getToolTipText(MouseEvent e) {
if (waferPlot != null){
Object source = e.getSource();
if (source instanceof WaferMapChartPanel){
WaferMapChartPanel chartSource= (WaferMapChartPanel)e.getSource();
Rectangle2D plotArea = chartSource.getChartRenderingInfo().getPlotInfo().getPlotArea();
Insets insets = this.getInsets();
double x = (e.getX() - insets.left) / this.getScaleX();
double y = (e.getY() - insets.top) / this.getScaleY();
return waferPlot.findChipAtPoint(x, y, plotArea);
}
}
return "";
}
}
This creates a tooltip of the die's x,y and bin or whatever value you are using instead of bin.

Cannot extract more than one metric from Google Analytics Reporting API V4 (Java)

I want to extract more than one Metric from Reporting API V4 Java classes. I pass from "main" function the "ga:" names of dimensions and metrics to the function below:
private static ReportRequest createComplexRequest(java.lang.String startDate,java.lang.String endDate,ArrayList<java.lang.String> metrics,ArrayList<java.lang.String> dimensions, String pageToken){
DateRange dateRange = new DateRange();
dateRange.setStartDate(startDate);
dateRange.setEndDate(endDate);
//Create the Metric object ArrayList
int metricsSize=metrics.size();
Metric[] metricsArray = new Metric[metricsSize];
for(int i=0;i<metricsSize;i++){
Metric metric = new Metric()
.setExpression("ga:"+metrics.get(i))
.setAlias(metrics.get(i));
metricsArray[i]=metric;
}
int dimensionsSize=dimensions.size();
Dimension[] dimensionsArray = new Dimension[dimensionsSize];
for(int i=0;i<dimensionsSize;i++){
Dimension dimension = new Dimension()
.setName("ga:"+dimensions.get(i));
dimensionsArray[i]=dimension;
}
ReportRequest request = new ReportRequest()
.setViewId(VIEW_ID)
.setDateRanges(Arrays.asList(dateRange))
.setDimensions(Arrays.asList(dimensionsArray))
.setMetrics(Arrays.asList(metricsArray))
.setPageToken(pageToken)
.setPageSize(10000);
return request;
}
As an example, I'm trying to extract "sessions" and "bounces" metrics. While I was trying to troubleshoot this case I realized that the metricHeaders.size() is 2 (as many as my metrics) but row.getMetrics() returns only one ("session" metric). Below is what I mean. I intentionally created the following problematic print function which reveals the problem:
private static void printResponse(GetReportsResponse response) {
for (Report report: response.getReports()) {
ColumnHeader header = report.getColumnHeader();
List<String> dimensionHeaders = header.getDimensions();
List<MetricHeaderEntry> metricHeaders = header.getMetricHeader().getMetricHeaderEntries();
List<ReportRow> rows = report.getData().getRows();
if (rows == null) {
System.out.println("No data found for " + VIEW_ID);
return;
}
for (ReportRow row: rows) {
List<String> dimensions = row.getDimensions();
List<DateRangeValues> metrics = row.getMetrics();
System.out.println("Metrics size:"+metrics.size());
for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {
System.out.println(dimensionHeaders.get(i) + ": " + dimensions.get(i));
}
for(int l = 0;l<metricHeaders.size();l++)
{
System.out.println(metricHeaders.get(l).getName());
DateRangeValues values = metrics.get(l);
}
}
}
}
The above function crushes because metricHeaders.size() is 2 but metrics is List with only one metric, the "sessions" metric!! That's why I get:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
Do you have any idea why this happens??? Please help!!
The sample printResponse() method in GA developers guide has some errors.
Try the following code instead:
private static void printResponseCustom(GetReportsResponse response) {
for (Report report: response.getReports()) {
ColumnHeader header = report.getColumnHeader();
List<String> dimensionHeaders = header.getDimensions();
List<MetricHeaderEntry> metricHeaders = header.getMetricHeader().getMetricHeaderEntries();
List<ReportRow> rows = report.getData().getRows();
if (rows == null) {
System.out.println("No data found for " + VIEW_ID);
return;
}
for (ReportRow row: rows) {
List<String> dimensions = row.getDimensions();
List<DateRangeValues> metrics = row.getMetrics();
System.out.println("Metrics size:"+metrics.size());
System.out.println("Metrics headers:"+metricHeaders.size());
for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {
System.out.println(dimensionHeaders.get(i) + ": " + dimensions.get(i));
}
int k = 0;
for (int j = 0; j < metricHeaders.size() && j < metrics.size();
DateRangeValues values = metrics.get(j);
for (String value : values.getValues()) {
MetricHeaderEntry entry = metricHeaders.get(k);
System.out.println(entry.getName() + ": " + value);
k++;
}
}
}
}
}

Read text file saved in SD card line by line

I have data.txt file saved in SD card containing values of variables required for driving 3 stepper motors. I want to read data.txt file line by line, split the line by "," and save each values into respective variables. data.txt file has data something like this:
8.24, 5.67, 7.34
3,86, 3.56, 4.49
5.38, 6.29, 3.67
I want to save value of first integer in x, second in y and third in z and execute the code. Once the code is executed, I want to get next line and load respective values in their respective variables and continue the loop till there is no line left in data.txt file
This code for matrix, let change some type that you need
public Matrix loadIntMapData(InputStream input) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line;
ArrayList<double[]> list = new ArrayList<double[]>();
while ((line = reader.readLine()) != null) {
if (line.equals("")) {
//ignore
} else {
String[] vs = line.split(",");
double ints[] = new double[vs.length];
for (int i = 0; i < ints.length; i++) {
ints[i] = Double.parseDouble(vs[i]);
}
list.add(ints);
}
}
double[][] map = new double[list.size()][];
for (int i = 0; i < map.length; i++) {
map[i] = list.get(i);
}
return new Matrix(map);
}
public static final void writInteMapData(Matrix matrix, File file) throws IOException {
double[][] map = matrix.getArray();
StringBuffer output = new StringBuffer();
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
output.append(Double.toString(map[i][j]));
if (j != map[i].length - 1) {
output.append(",");
}
}
if (i != map.length - 1) {
output.append("\r\n");
}
}
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(output.toString());
writer.close();
}

Resources