Merging MultiLineStrings - openlayers-3

I have few OL3 MultiLineString objects like
Object { type: "MultiLineString", coordinates: Array[2] }
Object { type: "MultiLineString", coordinates: Array[3] }
Object { type: "MultiLineString", coordinates: Array[4] }
Object { type: "MultiLineString", coordinates: Array[3] }
Now I want to merge all of them in a new big MultiLineString ( like PostGIS ST_Union function ). Is there some way to do this using OL3 or I must deal with JS arrays?

Have you look at the library JSTS
Personnaly, I use this library to make an union on two geometry.
var parser = new jsts.io.OL3Parser();
var a = parser.read(first_OlFeature.getGeometry());
var b = parser.read(second_OlFeature.getGeometry());
var unionGeometry = a.union(b);
var featureFromUnion = new ol.Feature().setGeometry(parser.write(unionGeometry));

Until better solution:
var newMultiline = {};
newMultiline.type = 'MultiLineString';
var newCoordinates = [];
for(x=0; x < routeData.length; x++ ) {
var geom = routeData[x].geometry;
for (z=0; z<geom.coordinates.length; z++ ) {
newCoordinates.push( geom.coordinates[z] )
}
}
newMultiline.coordinates = newCoordinates;

Related

Multidimensional Array in Realm Swift (Maximum depth exceeded error)

I want to make a 3D Array with Realm and I use List to accomplish this.
This is the code for my RealmDatabase:
class Sensors : Object {
dynamic var type = ""
dynamic var name = ""
let buttonAr = List<buttons>()
}
class buttons: Object{
let buttonsAdded = List<objectNameTopic>()
}
class objectNameTopic: Object {
dynamic var name = ""
dynamic var topic = ""
}
And this is what I use to call it:
var saving = Sensors()
var saving2 = objectNameTopic()
var but = buttons()
var array = ["on", "off"]
var array2 = ["1","2"]
var allSensors = useOfRealm.objects(Sensors.self)
override func viewDidLoad() {
super.viewDidLoad()
addSensor()
for i in 0...array.count-1 {
addSensor2(name: array[i], topic: array2[i])
}
if allSensors.count > 0 {
print(allSensors)
}
}
func addSensor() {
try! useOfRealm.write {
saving.type = "topic1"
saving.name = "mike"
useOfRealm.add(saving)
}
}
func addSensor2(name: String, topic: String) {
try! useOfRealm.write {
saving2.name = name
saving2.topic = topic
useOfRealm.add(saving2)
but.buttonsAdded.append(saving2)
saving.buttonAr.append(but)
}
}
This is what I get when I print the results:
Results<Sensors> (
[0] Sensors {
type = topic1;
name = mike;
buttonAr = RLMArray <0x6100000fd900> (
[0] buttons {
buttonsAdded = RLMArray <0x6100000fdb00> (
[0] <Maximum depth exceeded>,
[1] <Maximum depth exceeded>
);
},
[1] buttons {
buttonsAdded = RLMArray <0x6180000fd180> (
[0] <Maximum depth exceeded>,
[1] <Maximum depth exceeded>
);
}
);
}
)
Any Ideas of what I'm missing?
Thanks in advance
If the <Maximum depth exceeded> in the XCode console output is what you fear, fear no more. It's just a way the console is telling you that the object hierarchy is too deep for showing it to you, but the object(s) are there accessible through code.
Just try to print the content of your result by iterating through it and printing each child object instead of printing only the root.

How to convert a OPENLAYERS 3 postcompose method to a normal one?

I am trying to animate a line based on the given coordinates array comprising of latitude and longitude and I want to call my function just once and my coordinates name is: replayData.
map.on('postcompose', function (event) {
var vectorContext = event.vectorContext;
var frameState = event.frameState;
vectorContext.setFillStrokeStyle(null, animatedLineStroke);
var features = lineVectorSource.getFeatures();
for (var k = 0; k < features.length; k++) {
var feature = features[k];
if (!feature.get('finished')) {
var coords = feature.getGeometry().getCoordinates();
var elapsedTime = frameState.time - feature.get('start');
var elapsedPoints = elapsedTime * pointsPerMs;
if (elapsedPoints >= coords.length) {
feature.set('finished', true);
}
var maxIndex = Math.min(elapsedPoints, coords.length);
var currentLine = new ol.geom.LineString(coords.slice(0, maxIndex));
if (feature.get('iValue') == undefined || feature.get('iValue') < k) {
// drawMovingCarMarker(coords, k);
feature.set('iValue', k)
}
vectorContext.drawLineStringGeometry(currentLine, feature);
}
}
frameState.animate = true;
});
What this function is doing is first collecting all the values from for loop and then starting the animation. And because of this if I've 5 points the line between first two points will be drawn 5 times then 4,3, and so on.
Any help would be entertained. Thanks in advance.

How can I combine highcharts with different but similar x-axis categories?

I have 2 highcharts like this:http://jsfiddle.net/bqnkeLx9/
And I want to be able combine them into something like this: http://jsfiddle.net/gy2yo184/
Basically, is there an easy way to join 2 arrays together based on another array?
var category1 = [1,2,4,6,9]
var arr1 = [1,2,3,4,5]
var category2 = [1,3,6,8]
var arr2 = [6,7,8,9]
//do something to end with:
var allcategories = [1,2,3,4,6,8,9]
arr1 = [1,2,0,3,4,0,5]
arr2 = [6,0,7,0,8,9,0]
or maybe there is an easier way to plot this?
I don't think there is an easy way to do it. Or rather easier than:
making unique array of categories
manually filling up array with 0 values, when category is missing
For example:
// union without diplicates:
// taken from:
// http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items
Array.prototype.unique = function() {
var a = this.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i] === a[j])
a.splice(j--, 1);
}
}
return a;
};
And now use case:
// arrays:
var cats_1 = [1,2,4,6,9],
cats_2 = [1,3,6,8],
data_1 = [1,2,3,4,5],
data_2 = [6,7,8,9],
data_1_modified = [],
data_2_modified = [],
categories;
// get cateogries
categories = cats_1.concat(cats_2).unique();
// you may want to sort categories
categories.sort(function(a, b) {
return a - b;
});
categories.map(function(e, i) {
var index;
if( (index = cats_1.indexOf(e)) == -1) {
data_1_modified.push(0); // missing category
} else {
data_1_modified.push(data_1[index]);
}
if( (index = cats_2.indexOf(e)) == -1) {
data_2_modified.push(0); // missing category
} else {
data_2_modified.push(data_2[index]);
}
return e;
});
$('#container1').highcharts({
xAxis: {
categories: categories
},
series: [{
data: data_1_modified
}, {
data: data_2_modified
}]
});
And live demo: http://jsfiddle.net/bqnkeLx9/1/
you can Use javascript function concat() to join two array.
assuming here is our arrays
var category1 = [1,2,4,6,9]
var arr1 = [1,2,3,4,5]
var category2 = [1,3,6,8]
var arr1 = [1,2,3,4,5]
var category = category1.concat(category1);
the elements of array category will be something like as below
[1,2,4,6,9,8]
now you can pass array in highchart
Update
After removing all duplicate elements and after sorting final array is stored in arrayuniqueCat
$(function () {
var category1 = [1,2,4,6,9];
var category2 = [6,3,1,8];
var allcategories = category1.concat(category2);
var arr1 = [2,2,0,3,4,0,5];
var arr2 = [6,0,7,0,8,9,0];
var uniqueCat = allcategories.filter(function(elem, index, self) {
return index == self.indexOf(elem);
}).sort();
$('#container1').highcharts({
xAxis: {
categories: uniqueCat
},
series: [{
data: arr1
},{
data: arr2
}]
});
});
fiddle

Swift append Array in For Loop

let me start with the code:
var lat1:CLLocationDegrees = 50.102760
var lat2:CLLocationDegrees = -26.135170
.
.
.
var lat191:CLLocationDegrees = 60.289139
var LocCount = 191
for var iLat = 3; iLat == LocCount; ++iLat{
AirportLat.append("lat" + iLat)
}
What i try is go to count up the name of the var that should be appended to my array is there a way to do this?
Well, that's a difficult way to do it.
How about:
var latArray = <CLLocationDegrees>()
latArray += [50.102760]
latArray += [-26.135170]
.
.
.
latArray += [60.289139]
println("LocCount check = \(latArray.count)")
A better still way to do it would be to put the latitudes into a file (so you can change them, add to them, etc. without recompiling your code) and read them in, but I'll leave that as an exercise...
...and if you want to use the other answer's dictionary idea, you can add
for i in 0 ..< latArray.count {
dict["lat\(i+1)"] = latArray[i]
}
First off its not good practice to define 191 variables. Use Dictionary instead where keys are: lat1, lat2 .... lat191.
After that you can loop over Dictionary values and manipulate with them.
But if you still want to aggregate all variables to Array you can use class_copyIvarList to fetch all variables start with lat and write as:
class Myclass : NSObject{
var lat1:CLLocationDegrees = 50.102760
var lat2:CLLocationDegrees = -26.135170
var lat3:CLLocationDegrees = 60.289139
var AirportLat:Array<CLLocationDegrees> = []
func fetchValues() -> Array<CLLocationDegrees>{
var aClass : AnyClass? = self.dynamicType
var propertiesCount : CUnsignedInt = 0
let propertiesInAClass : UnsafeMutablePointer<Ivar> = class_copyIvarList(aClass, &propertiesCount)
for var i = 0; i < Int(propertiesCount); i++ {
var propName:String = NSString(CString: ivar_getName(propertiesInAClass[Int(i)]), encoding: NSUTF8StringEncoding)
var propValue : AnyObject! = self.valueForKey(propName)
if propName.hasPrefix("lat") {
AirportLat.append(propValue as CLLocationDegrees)
}
}//for
return AirportLat
}
}
var cc = Myclass()
cc.fetchValues()
Output:
[50.10276, -26.13517, 60.289139]
Comment: the class must inherit NSObject

Highcharts : categories in format with double quotes

I have series in the javascript using below code. Getting correct values but it is in Double quotes. So that is the reason I am not getting drilled down chart.
var arrayCodeSeries = <%= serializer.Serialize(ViewData["arrayCodeSeries"]) %>;
i = 0;
for(i=0;i<arrayCodeSeries.length;i++)
{
var temp = arrayCodeSeries[i];
temp = '['.concat(temp,']');
arrayCodeSeries[i] = temp;
}
I am getting
arrayCodeSeries[0] = "['70-158','70-177','70-181']"
data = [{
y: parseInt(arrayTotalCertificateCount[0]),
color: colors[0],
drilldown: {
name: 'Certificate Code',
categories: arrayCodeSeries[0],
data: [2,2,1],
color: colors[0]
}
I tried to remove double quotes with Replace, ReplaceAll and substring functions but it gets me error like it doesn't support these functions.
Help...
Thank you..
attaching here the array :
JS Fiddle with Static data http://jsfiddle.net/xj6ms/1/
According to comment discussion:
You should change your code from:
for(i=0;i<arrayCodeSeries.length;i++) {
var temp = arrayCodeSeries[i];
temp = '['.concat(temp,']');
arrayCodeSeries[i] = temp;
}
to:
var temp = [];
for(i=0;i<arrayCodeSeries.length;i++) {
var items = arrayCodeSeries[i].split(',');
temp.push(items);
}

Resources