Skip to content

Commit

Permalink
Merge pull request #13 from matafokka/development
Browse files Browse the repository at this point in the history
Rectangle Layer Update
  • Loading branch information
matafokka authored Dec 3, 2021
2 parents e2c9f7e + b50cda0 commit e59a305
Show file tree
Hide file tree
Showing 39 changed files with 15,454 additions and 7,553 deletions.
22 changes: 11 additions & 11 deletions ESRIGridParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ const MathTools = require("./MathTools.js");
const proj4 = require("proj4");

/**
* A stateful ESRI Grid parser. Calculates min and max values for selected polygons in {@link L.ALS.SynthGridLayer}. Can parse huge files by chunks.
* A stateful ESRI Grid parser. Calculates min and max values for selected polygons in {@link L.ALS.SynthPolygonLayer}. Can parse huge files by chunks.
*/
class ESRIGridParser {

/**
* Constructs a ESRI Grid parser.
*
* @param layer {L.ALS.SynthGridLayer} A layer to apply parsed values to.
* @param layer {L.ALS.SynthPolygonLayer} A layer to apply parsed values to.
* @param projectionString {string} Proj4 projection string. If not given, WGS84 assumed.
*/
constructor(layer = undefined, projectionString = "") {

/**
* Layer to apply parsed values to
* @type {L.ALS.SynthGridLayer}
* @type {L.ALS.SynthPolygonLayer}
*/
this.layer = layer

Expand Down Expand Up @@ -263,7 +263,7 @@ class ESRIGridParser {
*
* This method is NOT thread-safe! Call it outside of your WebWorker and pass your layer as an argument.
*
* @param layer {L.ALS.SynthGridLayer} If you're not using it in a WebWorker, don't pass anything. Otherwise, pass your layer.
* @param layer {L.ALS.SynthPolygonLayer} If you're not using it in a WebWorker, don't pass anything. Otherwise, pass your layer.
*/
copyStats(layer = undefined) {
let l = this.layer || layer;
Expand Down Expand Up @@ -314,15 +314,15 @@ class ESRIGridParser {

/**
* Generates initial parameters for the layer.
* @param layer {L.ALS.SynthGridLayer} Layer to copy data from
* @param layer {L.ALS.SynthPolygonLayer} Layer to copy data from
*/
static getInitialData(layer) {
let polygonsCoordinates = {};
for (let name in layer.selectedPolygons) {
if (!layer.selectedPolygons.hasOwnProperty(name))
for (let name in layer.polygons) {
if (!layer.polygons.hasOwnProperty(name))
continue;

let rect = layer.selectedPolygons[name].getBounds();
let rect = layer.polygons[name].getBounds();
polygonsCoordinates[name] = [
[rect.getWest(), rect.getNorth()],
[rect.getEast(), rect.getSouth()]
Expand All @@ -333,17 +333,17 @@ class ESRIGridParser {

/**
* Copies stats from any ESRIGridParser to a given layer
* @param layer {L.ALS.SynthGridLayer} Layer to copy stats to
* @param layer {L.ALS.SynthPolygonLayer} Layer to copy stats to
* @param stats {Object} Stats from any ESRIGridParser
*/
static copyStats(layer, stats) {
for (let name in stats) {
let widgetable = layer.selectedPolygonsWidgets[name];
let widgetable = layer.polygonsWidgets[name];
let s = stats[name];
widgetable.getWidgetById("minHeight").setValue(s.min);
widgetable.getWidgetById("maxHeight").setValue(s.max);
}
layer.updateGrid();
layer.updateAll();
}

}
Expand Down
45 changes: 19 additions & 26 deletions GeoTIFFParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ module.exports = async function (file, projectionString, initialData) {
let image = await tiff.getImage(i);

// Get info about image
let origin = image.getOrigin(), leftX = origin[0], topY = origin[1];
let nodata = image.getGDALNoData();
let resolution = image.getResolution(), xSize = resolution[0], ySize = resolution[1], zScale = resolution[2];
let [leftX, topY] = image.getOrigin(),
nodata = image.getGDALNoData(),
resolution = image.getResolution(), xSize = resolution[0], ySize = resolution[1], zScale = resolution[2];
if (zScale === 0)
zScale = 1;
let rightX = leftX + image.getWidth() * xSize, bottomY = topY + image.getHeight() * ySize;
let imagePolygon = turfHelpers.polygon([[
[leftX, topY], [rightX, topY], [rightX, bottomY], [leftX, bottomY], [leftX, topY]
]]);
let rightX = leftX + image.getWidth() * xSize, bottomY = topY + image.getHeight() * ySize,
imagePolygon = turfHelpers.polygon([[
[leftX, topY], [rightX, topY], [rightX, bottomY], [leftX, bottomY], [leftX, topY]
]]),
projInformation;

let projInformation;
if (projectionString === "") {
projInformation = toProj4.toProj4(image.getGeoKeys());
projectionString = projInformation.proj4;
Expand All @@ -42,12 +42,12 @@ module.exports = async function (file, projectionString, initialData) {

for (let name in initialData) {
// Let's project each polygon to the image, get their intersection part and calculate statistics for it
let polygon = initialData[name];
let oldBbox = [
let polygon = initialData[name],
oldBbox = [
polygon[0], [polygon[1][0], polygon[0][1]], polygon[1], [polygon[0][0], polygon[1][1]], polygon[0]
];
],
newBbox = [];

let newBbox = [];
for (let point of oldBbox)
newBbox.push(projectionFromWGS.forward(point));

Expand All @@ -62,8 +62,7 @@ module.exports = async function (file, projectionString, initialData) {
continue;

intersection = intersection.geometry.coordinates[0];
let points = [intersection[0], intersection[2]];
let imageWindow = [];
let points = [intersection[0], intersection[2]], imageWindow = [];
for (let point of points) {
imageWindow.push(
Math.floor((point[0] - leftX) / xSize),
Expand All @@ -79,22 +78,16 @@ module.exports = async function (file, projectionString, initialData) {

// geotiff.js will mash all pixels into one array.
// To easily keep track of coordinates and reduce memory consumption, we need to read image row by row.
let minX = imageWindow[0], maxX = imageWindow[2];
let currentY = imageWindow[1], maxY = imageWindow [3];

// Stats for current polygon
let stats = {
min: Infinity,
max: -Infinity,
}
let [minX, currentY, maxX, maxY] = imageWindow,
stats = {min: Infinity, max: -Infinity} // Stats for current polygon

for (currentY; currentY <= maxY; currentY++) {
let currentX = minX;
let raster = await image.readRasters({ window: [minX, currentY, maxX, currentY + 1] });
let color0 = raster[0]; // Raster is a TypedArray where elements are colors and their elements are pixel values of that color.
let index = -1;
let raster = await image.readRasters({window: [minX, currentY, maxX, currentY + 1]});
let color0 = raster[0], // Raster is a TypedArray where elements are colors and their elements are pixel values of that color.
index = -1;
for (let pixel of color0) {
let crsX = origin[0] + currentX * xSize, crsY = origin[1] + currentY * ySize;
let crsX = leftX + currentX * xSize, crsY = topY + currentY * ySize;
if (projInformation) {
crsX *= projInformation.coordinatesConversionParameters.x;
crsY *= projInformation.coordinatesConversionParameters.y;
Expand Down
65 changes: 0 additions & 65 deletions SynthBase/SynthBaseDrawLayer.js

This file was deleted.

21 changes: 13 additions & 8 deletions SynthBase/SynthBaseLayer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require("./SynthBaseSettings.js");
const turfHelpers = require("@turf/helpers");
const MathTools = require("../MathTools.js");

Expand Down Expand Up @@ -145,19 +146,16 @@ L.ALS.SynthBaseLayer = L.ALS.Layer.extend(/** @lends L.ALS.SynthBaseLayer.protot

addBaseParametersOutputSection: function () {
let yWidgets = [];
if (this.hasYOverlay) {
yWidgets = [
new L.ALS.Widgets.ValueLabel("ly", "ly", "m"),
new L.ALS.Widgets.ValueLabel("Ly", "Ly", "m"),
new L.ALS.Widgets.ValueLabel("By", "By", "m"),
];
}
if (this.hasYOverlay)
yWidgets = [new L.ALS.Widgets.ValueLabel("By", "By", "m")];

let valueLabels = [
new L.ALS.Widgets.ValueLabel("flightHeight", "flightHeight", "m"),
new L.ALS.Widgets.ValueLabel("lx", "lx", "m"),
new L.ALS.Widgets.ValueLabel("Lx", "Lx", "m"),
new L.ALS.Widgets.ValueLabel("Bx", "Bx", "m"),
new L.ALS.Widgets.ValueLabel("ly", "ly", "m"),
new L.ALS.Widgets.ValueLabel("Ly", "Ly", "m"),
...yWidgets,
new L.ALS.Widgets.ValueLabel("timeBetweenCaptures", "timeBetweenCaptures", "s"),
new L.ALS.Widgets.ValueLabel("GSI", "GSI", "m"),
Expand Down Expand Up @@ -189,6 +187,7 @@ L.ALS.SynthBaseLayer = L.ALS.Layer.extend(/** @lends L.ALS.SynthBaseLayer.protot
layer.setRadius(doubleThickness);
})
}
this.updateDrawThickness();
},

_setPathsColor: function () {
Expand Down Expand Up @@ -249,7 +248,7 @@ L.ALS.SynthBaseLayer = L.ALS.Layer.extend(/** @lends L.ALS.SynthBaseLayer.protot
_createPathWidget: function (layer, length, toFlash) {
let button = new L.ALS.Widgets.Button("flashPath", "flashPath", this, "flashPath"),
lengthWidget = new L.ALS.Widgets.ValueLabel("pathLength", "pathLength", "m").setFormatNumbers(true).setNumberOfDigitsAfterPoint(0),
timeWidget = new L.ALS.Widgets.ValueLabel("flightTime", "flightTime", "h:mm").setValue(),
timeWidget = new L.ALS.Widgets.ValueLabel("flightTime", "flightTime", "h:mm"),
warning = new L.ALS.Widgets.SimpleLabel("warning", "", "center", "warning"),
widget = new L.ALS.Widgets.Spoiler(`pathWidget${this._pathsWidgetsNumber}`, `${L.ALS.locale.pathSpoiler} ${this._pathsWidgetsNumber}`)
.addWidgets(button, lengthWidget, timeWidget, warning);
Expand Down Expand Up @@ -444,8 +443,14 @@ L.ALS.SynthBaseLayer = L.ALS.Layer.extend(/** @lends L.ALS.SynthBaseLayer.protot
line.isFlashing = false;
},

clearSerializedPathsWidgets: function (serialized) {
for (let i = 1; i <= this._pathsWidgetsNumber; i++)
delete serialized._widgets["pathWidget" + i];
}

});

require("./Hull.js");
require("./calculateParameters.js");
require("./draw.js");
require("./toGeoJSON.js");
12 changes: 8 additions & 4 deletions SynthBase/calculateParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ L.ALS.SynthBaseLayer.prototype.calculateParameters = function () {
this.ly = this["cameraWidth"] * pixelWidth; // Image size in meters
this.Ly = this.ly * this["imageScale"] // Image width on the ground

if (this.hasYOverlay)
this.By = this.Ly * (100 - this["overlayBetweenPaths"]) / 100; // Distance between paths

this.lx = this["cameraHeight"] * pixelWidth; // Image height
this.Lx = this.lx * this["imageScale"]; // Image height on the ground
this.Bx = this.Lx * (100 - this["overlayBetweenImages"]) / 100; // Capture basis, distance between images' centers
Expand All @@ -36,7 +33,14 @@ L.ALS.SynthBaseLayer.prototype.calculateParameters = function () {

this.timeBetweenCaptures = this.Bx / this.aircraftSpeedInMetersPerSecond;

let names = ["flightHeight", "lx", "Lx", "Bx", "ly", "Ly", "By", "GSI", "IFOV", "GIFOV", "FOV", "GFOV", "timeBetweenCaptures"];
let names = ["flightHeight", "lx", "Lx", "Bx", "ly", "Ly", "GSI", "IFOV", "GIFOV", "FOV", "GFOV", "timeBetweenCaptures"];


if (this.hasYOverlay) {
this.By = this.Ly * (100 - this["overlayBetweenPaths"]) / 100; // Distance between paths
names.push("By");
}

for (let name of names) {
const field = this[name];
if (field === undefined)
Expand Down
65 changes: 65 additions & 0 deletions SynthBase/draw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Enables L.Draw on this layer
* @param drawControls {Object} L.Draw controls to use
* @param drawingGroup {L.FeatureGroup} Group to pass to L.Draw
*/
L.ALS.SynthBaseLayer.prototype.enableDraw = function (drawControls, drawingGroup) {

/**
* Leaflet.Draw controls to use
*/
this.drawControls = drawControls;

this._drawingGroup = drawingGroup;

this._drawTypes = ["circle", "circlemarker", "marker", "polygon", "polyline", "rectangle", "simpleshape"];

this._drawOptions = {
draw: {},
edit: {
featureGroup: this._drawingGroup,
remove: true,
}
}

for (let control of this._drawTypes)
this._drawOptions.draw[control] = false;

for (let control in this.drawControls)
this._drawOptions.draw[control] = this.drawControls[control];

this.drawControl = new L.Control.Draw(this._drawOptions);

this.updateDrawThickness();
this.addEventListenerTo(this.map, "draw:created", "onDraw");
this.addEventListenerTo(this.map, "draw:drawstart draw:editstart draw:deletestart", "onEditStart");
this.addEventListenerTo(this.map, "draw:drawstop draw:editstop draw:deletestop", "onEditEnd");
this.addControl(this.drawControl, "top", "topleft");
}

L.ALS.SynthBaseLayer.prototype.onDraw = function (e) {
this._drawingGroup.addLayer(e.layer);

let borderColorId, fillColor;

if (e.layer instanceof L.Polygon || e.layer instanceof L.CircleMarker || e.layer instanceof L.Marker) {
borderColorId = "borderColor";
fillColor = this.getWidgetById("fillColor").getValue();
} else {
borderColorId = "color0";
fillColor = "transparent";
}

e.layer.setStyle({color: this.getWidgetById(borderColorId).getValue(), fillColor, opacity: 1});
}

L.ALS.SynthBaseLayer.prototype.onEditStart = function () {}
L.ALS.SynthBaseLayer.prototype.onEditEnd = function () {}

L.ALS.SynthBaseLayer.prototype.updateDrawThickness = function () {
for (let type of this._drawTypes) {
try {
this._drawOptions.draw[type].shapeOptions.weight = this.lineThicknessValue;
} catch (e) {}
}
}
Loading

0 comments on commit e59a305

Please sign in to comment.