openlayers 3 IGC source from text - maps

I am using openlayers 3 and I am looking for a way to load track data not from urls, but directly by adding text to vectorsource IGC. I have the data with coordinates and times as arrays. What format is right? So, insted of this:
var vectorSource = new ol.source.IGC({
projection: 'EPSG:3857',
urls: [
'data/igc/Clement-Latour.igc',
'data/igc/Damien-de-Baenst.igc',
'data/igc/Sylvain-Dhonneur.igc',
'data/igc/Tom-Payne.igc',
'data/igc/Ulrich-Prinz.igc'
]
});
I'd like to use something like this (not sure about the format):
var vectorSource = new ol.source.IGC({
projection: 'EPSG:3857',
text: [
array1,
array2,
array3 // or arrayall with all three arrays, or text.. What is correct?
]
});

For more control and flexibility you can use an ol.source.Vector and an ol.format.IGC instead of the "convenient" ol.source.IGC. For example:
var source = new ol.source.Vector();
var format = new ol.format.IGC();
var readOptions = {featureProjection: 'EPSG:3857'};
var features = [
format.readFeature(text1, readOptions),
format.readFeature(text2, readOptions),
format.readFeature(text3, readOptions)
];
source.addFeatures(features);
var layer = new ol.layer.Vector({source: source});

Related

Feeding array to json for use in turf.polygon, structure problems

I have a linestring and a polygon and I am using turf.booleanIntersect() to determine if the line goes through the polygon. The example that i have tested and works is:
var poly1 = turf.polygon([
[
[148.535693, -29.6],
[154.553967, -29.64038],
[154.526554, -33.820031],
[148.535693, -33.6],
[148.535693, -29.6]
]
]);
//const p1 = L.geoJSON(poly1).addTo(mymap);
console.log("TEST: " + turf.booleanIntersects(line, poly1));
In my real code I read the polygon values from a file and need to insert them into an array which needs to be converted into a "GeoJSON Feature or Geometry" (from webpage).
I am having trouble getting the array to json convert correct.
var polygonlines = [];
var start = [long,lat];
polygonlines.push([start]); //add multiple of these points to the to polygonlines array
//create my json
var geojsonPolygon =
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": polygonlines
}
}
var turfpolygon = turf.polygon(geojsonPolygon.data.geometry.coordinates); //ERROR HERE
const p2 = L.geoJSON(turfpolygon).addTo(mymap);
var result = turf.booleanIntersects(line, turfpolygon)
The error I get is "Uncaught Error Error: Each LinearRing of a Polygon must have 4 or more Positions."
I can't quite get the structure of the geojsonPolygon correct. I think that it is look at geojsonPolygon Array(1) in attached picture instead of Array(10), but I can't work out how to fix it.
Would love some help getting this structure fixed. Thank you :)
p.s. please ignore values of lat/longs, just examples.
I have seen this question but it hasn't helped How to feed JSON data of coordinates to turf.polygon?
Ongoing answer..., content and code will be edited.
Here is a LIVE demo code that you can run to see how it works. It may help to answer you question.
Usage:
click Run code snippet button
click Full page in top-right corner to see map and console
//Using turf_polygon object style
var poly1 = turf.polygon([
[
[148.535693, -29.6],
[154.553967, -29.64038],
[154.526554, -33.820031],
[148.535693, -33.6],
[148.535693, -29.6]
]
]);
// Using geojson style data
// Coordinates are slightly different from poly1
// This can be used as a good example to compare/contrast with your implementation
// This geojson of poly2 works, you can see it on the map.
var poly2 = {
type: "Feature",
properties: { id: 102, name: "Poly_2" },
geometry: {
type: "Polygon",
coordinates: [
[
[148, -29],
[154, -29],
[154, -33],
[148, -33],
[148, -29]
]
]
}
};
var line12 = turf.lineString([[144, -30], [153, -31.8], [159, -32]]);
var line34 = turf.lineString([[144, -20], [160, -30]]);
/* Init and draw Leaflet map */
var map;
function initMap(coords, zoom) {
// initialize map container
map = L.map("mapid").setView(coords, zoom);
// get the stamen toner-lite tiles
var Stamen_Toner = L.tileLayer(
"https://stamen-tiles-{s}.a.ssl.fastly.net/toner-lite/{z}/{x}/{y}.{ext}",
{
attribution:
'Map tiles by Stamen Design, CC BY 3.0 — Map data © OpenStreetMap',
subdomains: "abcd",
minZoom: 0,
maxZoom: 20,
ext: "png"
}
);
// add the tiles to the map
Stamen_Toner.addTo(map);
//disable scroll wheel zoom
map.scrollWheelZoom.disable();
}
/* Leaflet use (lat,long) */
var coordinates = [-30, 150]; //[lat,long]
var zoom = 5;
initMap(coordinates, zoom);
//Add polygons and lines
L.geoJson(turf.featureCollection([poly1, poly2, line12, line34])).addTo(map);
// Intersection op
var result1 = turf.booleanIntersects(line12, poly1); //True
var result2 = turf.booleanIntersects(line34, poly1); //False
console.log(result1, result2);
//EOF
#mapid { height: 480px; width: 800px}
<link rel="stylesheet" href="https://unpkg.com/leaflet#1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin="" />
<!-- Make sure you put this AFTER Leaflet's CSS -->
<script src="https://unpkg.com/leaflet#1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/6.5.0/turf.min.js"></script>
<div id="mapid"></div>
How to get the polygonline array into geojson for use in turf polygon:
var polygonlines = [];
//these 2 lines occur multiple times in a loop
{
var start = [vol.lines[k].start.lng, vol.lines[k].start.lat];
polygonlines.push(start);
}
var geojsonPolygon =
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [polygonlines]
}
var turfpolygon = turf.polygon(geojsonPolygon.geometry.coordinates);
turf.booleanIntersects(line, turfpolygon)

Creating array in Swift

I'm using Swift to create an array that needs to match this format:
let items = [["1", "red", "33", "canada"], ["2", "blue", "66", "usa"]]
In my code, I query a database and have multiple rows returned with the pertinent info:
let items = [id+" - "+colour+" - "+number+" - "+home_location]
I use a loop to .append the array but the format comes out like this instead:
["1 - red - 33 - canada", "2 - blue - 66 - usa"]
What do I need to do to create the required array structure?
For each row of the database, instead of
let items = [id+" - "+colour+" - "+number+" - "+home_location]
say
let items = [id, colour, number, home_location]
Now append that to a var array of [[String]].
create model for your data like this
class ModelClass {
var id = ""
var colour = ""
var number = ""
var home_location = ""
}
and then create the object of your model class like this
let objmodel : ModelClass = ModelClass()
objmodel.id = "1"
objmodel.colour = "red"
objmodel.number = "33"
objmodel.home_location = "canada"
then create your main array and append this model object to your model array
var arrData = [ModelClass]()
arrData.append(objmodel)

Copy Array from http request to other array in Angular

I pretty new to angular and I have a question.
I need 2 arrays for ng2-charts, one with labels and one with data.
I make http request to AWS and I got this json
{
"System1": [
{
"name": "MF3",
"descr": "Multifilo MF3",
"speed": [1,2,3,4],
"time": ["10.01", "10.02", "10.03", "10.04"]
}
]
}
I assing all the result to result: Array<SystemModel>;
For use speed and time on ng2-charts I have to copy the two array speed and time on new array: public lineChartSpeed: Array<number> = []; and public lineChartTime: Array<any> = [];
How can I copy this 2 array on my new array? I know how to access to data only on html template, but not on typscript file...
My component is:
public lineChartSpeed: Array<number> = [];
lineChartTime: Array<any> = [];
result: Array<ImpiantoModel>;
getdata() {
this.http.get<SystemModel[]>(this.myUrl)
.subscribe(
data => { this.result = data;
// perform the copy of speed and time on lineChartTime and lineChartSpeed
});
}
How can I copy the array?
If you need more details, please ask in the comments!
thank you !
var system1 = {
"System1": [
{
"name": "MF3",
"descr": "Multifilo MF3",
"speed": [1,2,3,4],
"time": ["10.01", "10.02", "10.03", "10.04"]
}
]
}
var speed = system1.System1[0].speed
var time = system1.System1[0].time
console.log('Array of Speed', speed)
console.log('Array of Time', time)
//Merge or concatenate two Arrays
var newArray = [...speed, ...time]
console.log('Merged or concatenated Arrays', newArray)
Use slice operator to create a new copy of the array
this.result = data.slice();
this.lineChartSpeed = [].concat(this.result[0].speed);
this.lineChartTime = [].concat(this.result[0].time);

Add subelement to JSON array

I have the following code where I design the format of a JSON-array dynamically from variables ==>
var Title = '"text_title"'
var Topic2 = '"text_topic2"'
var jsonData = [ ];
createWantedJSON("title", data[i].topic, jsonData)
function createWantedJSON(title, Topic2, arr) {
arr.push({
"Topic": title,
[Topic2] : {
"Topic3": [{
"Subitem1": "Text1",
"Subitem2": "Text2",
"Subitem3": "Text3"
}]
}
}
This goes fine for the key and value which are not nested. But I don't know how to make it dynamic for the content of Topic3. I tried making a variable with the following content =>
'"Subitem1": "Text1", "Subitem2": "Text2", "Subitem3": "Text3"' but then of course it sees the content as one string and not as elements of the nested Topic3 array. I guess this isn't that hard but I can't seem to fix it.

AngularJS: Trying to clean up an API Feed by processing $http response is not working

I have an API Feed of global variables to be used throughout an application. The raw feed looks like this:
[
{
id: "1",
var: "g_crop_year",
val: "2015"
},
{
id: "2",
var: "g_season",
val: "F"
}
]
To me, that is awkward to work with throughout an application and I would prefer it look like this:
[
{ g_crop_year: "2015" },
{ g_season: "F" }
]
I tried this:
$http.get('/globals')
.success(function(globals){
var simpleGlobals = [];
for(var g=0; g<globals.length;g++){
var glo = {
globals[g].var: globals[g].val
};
simpleGlobals.push(glo);
}
$scope.globals = simpleGlobals;
});
My thoughts were to loop through the returned data, create the structure I want and push to a new array which I assign to $scope. But I am wrong. I get a blank page and an unexpected token in the globals[g].var: globals[g].val line.
This will not work as you expect:
var glo = {
globals[g].var: globals[g].val
};
They key of the glo object is attempting be the literal text globals[g].var; change that, instead, to:
var glo = {};
glo[globals[g].var] = globals[g].val;
As a side note, you may want to assign to $rootScope or a Service to make these globals available to all of your controllers.
This is not the right approach to create your designated approach.
var glo = {
globals[g].var: globals[g].val
};
The string globals[g].var is actually being considered as the key rather than interpreting it as a value.
You should instead create a blank object first and then use the array annotation to assign a particular value to the key.
var glo = {};
glo[globals[g].var] = globals[g].val;

Resources