How to read external GeoJSON file from openlayers? - dictionary

I have to draw some lines by OpenLayers. The line features are coded as GeoJSON format. My code is ok for hard coded GeoJSON features. But, if I put this features in separate file and try to load it. It just does not work. I do not know what is the wrong with my loading external GeoJSON file. I have given both the code.
Code 1:
// This code is ok with hard coded GeoJSON features
map.addControl(new OpenLayers.Control.LayerSwitcher());
vectorLayer = new OpenLayers.Layer.Vector("Lines");
var myGeoJSON = { "type": "FeatureCollection",
"features":
[
{ "type": "Feature", "properties": { "LENGTH": 756.304000}, "geometry": { "type": "LineString", "coordinates": [ [ 18.105018, 59.231027 ], [ 18.104176, 59.230737 ], [ 18.103928, 59.230415 ], [ 18.103650, 59.230336 ], [ 18.103028, 59.230463 ], [ 18.102491, 59.230418 ], [ 18.101976, 59.230237 ], [ 18.100893, 59.230110 ], [ 18.100117, 59.230016 ], [ 18.097715, 59.230262 ], [ 18.096907, 59.230376 ], [ 18.096637, 59.230405 ], [ 18.096578, 59.230428 ], [ 18.096429, 59.230450 ], [ 18.096336, 59.230479 ], [ 18.096108, 59.230534 ], [ 18.095971, 59.230600 ], [ 18.095925, 59.230633 ], [ 18.095891, 59.230665 ], [ 18.094000, 59.231676 ], [ 18.093864, 59.231720 ] ] } }
,
{ "type": "Feature", "properties": { "LENGTH": 1462.390000}, "geometry": { "type": "LineString", "coordinates": [ [ 17.877073, 59.461653 ], [ 17.877116, 59.461598 ], [ 17.876936, 59.461507 ], [ 17.876936, 59.461323 ], [ 17.876773, 59.461098 ], [ 17.876430, 59.460885 ], [ 17.876413, 59.460553 ], [ 17.876576, 59.460280 ], [ 17.876575, 59.460078 ], [ 17.876762, 59.460060 ], [ 17.877371, 59.460042 ], [ 17.877808, 59.460046 ], [ 17.878641, 59.460046 ], [ 17.879010, 59.460078 ], [ 17.879337, 59.460044 ], [ 17.879526, 59.459878 ], [ 17.879749, 59.459563 ], [ 17.880058, 59.459538 ], [ 17.880435, 59.459503 ], [ 17.887550, 59.453608 ], [ 17.887696, 59.453430 ], [ 17.887971, 59.453150 ], [ 17.888221, 59.452843 ], [ 17.888246, 59.452721 ], [ 17.888435, 59.452609 ], [ 17.888470, 59.452568 ], [ 17.888517, 59.452410 ] ] } }
]
};
var geojson_format = new OpenLayers.Format.GeoJSON({
'internalProjection': map.baseLayer.projection,
'externalProjection': new OpenLayers.Projection("EPSG:4326")
});
map.addLayer(vectorLayer);
vectorLayer.addFeatures(geojson_format.read(myGeoJSON));
map.setCenter(
new OpenLayers.LonLat(18.068611, 59.329444).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 10
);
Code 2: This code shows an error that it could not load features
//This code does not work because it can not load the external GeoJSON file
map.addControl(new OpenLayers.Control.LayerSwitcher());
vectorLayer = new OpenLayers.Layer.Vector("Lines");
var myGeoJSON = new OpenLayers.Layer.Vector("Lines", {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "ml/lines.json"
})
});
var geojson_format = new OpenLayers.Format.GeoJSON({
'internalProjection': map.baseLayer.projection,
'externalProjection': new OpenLayers.Projection("EPSG:4326")
});
map.addLayer(vectorLayer);
vectorLayer.addFeatures(geojson_format.read(myGeoJSON));
map.setCenter(
new OpenLayers.LonLat(18.068611, 59.329444).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 10
);
Thanks in advance

geojson_layer = new OpenLayers.Layer.Vector("GeoJSON", {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "ml/lines.json",
format: new OpenLayers.Format.GeoJSON()
})
});
See my little example.

Related

Why is watcher giving errors?

I want to send slack notifications to a channel as soon as any log with loglevel ERROR appears in my index. I have configured watcher in the following way but it is giving me errors. The slack message must have the log message.
I am not able to configure this exactly.
{
"trigger": {
"schedule": {
"interval": "10s"
}
},
"input": {
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
"index-log*",
"index-beat*"
],
"rest_total_hits_as_int": true,
"body": {
"query": {
"match": {
"loglevel": "ERROR"
},
"range": {
"#timestamp": {
"from": "{{ctx.trigger.scheduled_time}}||-5m",
"to": "{{ctx.trigger.triggered_time}}"
}
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.hits.total": {
"gt": 0
}
}
},
"actions": {
"send_trigger": {
"webhook": {
"scheme": "https",
"host": "hooks.slack.com",
"port": 443,
"method": "post",
"path": "/services/XXXX/XXXX/XXXX",
"params": {},
"headers": {
"Content-type": "application/json"
},
"body": """{ "text": "{{ctx.payload}}"}"""
}
}
}
}
below is the structure of my logs in kibana
{
"_index": "index-beat",
"_type": "_doc",
"_id": "P3Toa34B1LVeuWotaVOY",
"_version": 1,
"_score": 1,
"_source": {
"#timestamp": "2022-01-18T06:38:19.559Z",
"name": "communication",
"loglevel": "ERROR",
"log": {
"file": {
"path": "/home/ubuntu/abc/abc/logs/communication.log"
},
"offset": 0
},
"timestamp": "2022-01-18T06:38:15.384279",
"exception": {
"ex_type": "None",
"ex": "None",
"tb": ""
},
"message": "{'err': 'Test'}"
},
"fields": {
"exception.ex_type": [
"None"
],
"loglevel.keyword": [
"ERROR"
],
"name.keyword": [
"communication"
],
"log.offset": [
0
],
"message": [
"{'err': 'Test'}"
],
"exception.tb": [
""
],
"exception.ex": [
"None"
],
"#timestamp": [
"2022-01-18T06:38:19.559Z"
],
"exception.tb.keyword": [
""
],
"loglevel": [
"ERROR"
],
"log.file.path": [
"/home/ubuntu/abc/abc/logs/communication.log"
],
"message.keyword": [
"{'err': 'Test'}"
],
"name": [
"communication"
],
"exception.ex_type.keyword": [
"None"
],
"exception.ex.keyword": [
"None"
],
"log.file.path.keyword": [
"/home/ubuntu/abc/abc/logs/communication.log"
],
"timestamp": [
"2022-01-18T06:38:15.384Z"
]
}
}
Please help me out in this one.

Way to convert json output jq

I have the following json output:
[
{
"serviceid": "service1",
"endpoints" : {
"endpoint1": [
"example1",
"example2",
"example3",
"example4"
],
"endpoint2": [
"example3",
"example4",
"example5",
"example6"
]
},
"version": "1.0"
},
{
"serviceid": "service2",
"endpoints" : {
"endpoint3": [
"example7",
"example8",
"example9",
"example10"
]
},
"version": "2.0"
}
]
So I need to remove endpoint1, endpoint2 and endpoint3 and preserve their children as array.
This is the desired output:
[
{
"serviceid": "service1",
"endpoints" : [
"example1",
"example2",
"example3",
"example4"
"example3",
"example4",
"example5",
"example6"
],
"version": "1.0"
},
"serviceid": "service2",
"endpoints" : [
"example7",
"example8",
"example9",
"example10"
],
"version": "2.0"
}
]
I don't have any idea to face it, please could you help me?
Thanks in advance.
Once you've fixed all the typos in your input (several commas and some curly braces were missing), this should work:
jq 'map(.endpoints |= [.[][]])'
Demo

Where can I view all the eslint ast node types?

There are many node types to detect, ie:
VariableDeclarator
FunctionExpression
MemberExpression
AssignmentExpression
The eslint website explains the rules but doesn't provide all of the available node type detections.
I found a tutorial example detecting IfStatement but this is not picking up my if statement, so wondering if I have a syntax error.
Besides the ESTree documentation that you found, I would recommend: https://astexplorer.net/
It shows you the AST for the code you paste in and also highlights which part of the code each node corresponds to as you click/hover over them.
It's invaluable when writing a rule, figuring out edge cases, or in general, for understanding how the AST of a given snippet of code looks like. Give it a try!
A more up to date list (e.g., that includes ArrowFunctionExpression) is at https://github.com/benjamn/ast-types/blob/master/gen/namedTypes.ts . https://github.com/benjamn/ast-types/blob/master/gen/kinds.ts and https://github.com/benjamn/ast-types/blob/master/def/es6.ts are also interesting and may be helpful as well.
It's ok. The reason my ifstatement wasn't working was because i didn't have an if statement in the code I was testing.
Regarding the reference to all the ast node types I found that too -
https://github.com/estree/estree/blob/master/es5.md
I belive this is what you want
AssignmentExpression: [ 'left', 'right' ],
AssignmentPattern: [ 'left', 'right' ],
ArrayExpression: [ 'elements' ],
ArrayPattern: [ 'elements' ],
ArrowFunctionExpression: [ 'params', 'body' ],
AwaitExpression: [ 'argument' ],
BlockStatement: [ 'body' ],
BinaryExpression: [ 'left', 'right' ],
BreakStatement: [ 'label' ],
CallExpression: [ 'callee', 'arguments' ],
CatchClause: [ 'param', 'body' ],
ClassBody: [ 'body' ],
ClassDeclaration: [ 'id', 'superClass', 'body' ],
ClassExpression: [ 'id', 'superClass', 'body' ],
ConditionalExpression: [ 'test', 'consequent', 'alternate' ],
ContinueStatement: [ 'label' ],
DebuggerStatement: [],
DoWhileStatement: [ 'body', 'test' ],
EmptyStatement: [],
ExportAllDeclaration: [ 'source' ],
ExportDefaultDeclaration: [ 'declaration' ],
ExportNamedDeclaration: [ 'declaration', 'specifiers', 'source' ],
ExportSpecifier: [ 'exported', 'local' ],
ExpressionStatement: [ 'expression' ],
ExperimentalRestProperty: [ 'argument' ],
ExperimentalSpreadProperty: [ 'argument' ],
ForStatement: [ 'init', 'test', 'update', 'body' ],
ForInStatement: [ 'left', 'right', 'body' ],
ForOfStatement: [ 'left', 'right', 'body' ],
FunctionDeclaration: [ 'id', 'params', 'body' ],
FunctionExpression: [ 'id', 'params', 'body' ],
Identifier: [],
IfStatement: [ 'test', 'consequent', 'alternate' ],
ImportDeclaration: [ 'specifiers', 'source' ],
ImportDefaultSpecifier: [ 'local' ],
ImportExpression: [ 'source' ],
ImportNamespaceSpecifier: [ 'local' ],
ImportSpecifier: [ 'imported', 'local' ],
JSXAttribute: [ 'name', 'value' ],
JSXClosingElement: [ 'name' ],
JSXElement: [ 'openingElement', 'children', 'closingElement' ],
JSXEmptyExpression: [],
JSXExpressionContainer: [ 'expression' ],
JSXIdentifier: [],
JSXMemberExpression: [ 'object', 'property' ],
JSXNamespacedName: [ 'namespace', 'name' ],
JSXOpeningElement: [ 'name', 'attributes' ],
JSXSpreadAttribute: [ 'argument' ],
JSXText: [],
JSXFragment: [ 'openingFragment', 'children', 'closingFragment' ],
Literal: [],
LabeledStatement: [ 'label', 'body' ],
LogicalExpression: [ 'left', 'right' ],
MemberExpression: [ 'object', 'property' ],
MetaProperty: [ 'meta', 'property' ],
MethodDefinition: [ 'key', 'value' ],
NewExpression: [ 'callee', 'arguments' ],
ObjectExpression: [ 'properties' ],
ObjectPattern: [ 'properties' ],
Program: [ 'body' ],
Property: [ 'key', 'value' ],
RestElement: [ 'argument' ],
ReturnStatement: [ 'argument' ],
SequenceExpression: [ 'expressions' ],
SpreadElement: [ 'argument' ],
Super: [],
SwitchStatement: [ 'discriminant', 'cases' ],
SwitchCase: [ 'test', 'consequent' ],
TaggedTemplateExpression: [ 'tag', 'quasi' ],
TemplateElement: [],
TemplateLiteral: [ 'quasis', 'expressions' ],
ThisExpression: [],
ThrowStatement: [ 'argument' ],
TryStatement: [ 'block', 'handler', 'finalizer' ],
UnaryExpression: [ 'argument' ],
UpdateExpression: [ 'argument' ],
VariableDeclaration: [ 'declarations' ],
VariableDeclarator: [ 'id', 'init' ],
WhileStatement: [ 'test', 'body' ],
WithStatement: [ 'object', 'body' ],
YieldExpression: [ 'argument' ]

Analytics Core Reporting API. Rows in different segments have same values

I probably have some misconception, so need help with understanding what is exactly wrong.
At analytics.google.com I have two segments they differ by event label but both are from the same event. In analytics interface difference between segments is visible:
When I'm trying to get this same data with tool like query explorer results are also different:
However, when I'm getting this data with reporting API v4, all values are the same between segments. I have also tried it with API v3, which resulted in similar outcomes.
Here is my code:
googleapis.analyticsreporting('v4').reports.batchGet({
'headers': {'Content-Type': 'application/json'},
"auth": oauth2Client,
"resource":{
reportRequests:[
{
"viewId": "ga:"+Meteor.settings.admin.googleAPI.viewID,
"dateRanges":[{
"startDate": '2016-07-01',
"endDate": moment().format('YYYY-MM-DD'),
}],
"metrics": [{"expression":"ga:pageviews"},{"expression":"ga:avgtimeonpage"}],
"dimensions": [{"name":"ga:pagepath"},{"name":"ga:segment"}],
"segments": [{
"dynamicSegment":
{
"name": "version_bw",
"userSegment":
{
"segmentFilters": [
{
"simpleSegment":
{
"orFiltersForSegment": [
{
"segmentFilterClauses": [
{
"dimensionFilter":
{
"dimensionName": "ga:eventAction",
"operator": "EXACT",
"expressions": ["set-visual-code"]
}
},{
"dimensionFilter":
{
"dimensionName": "ga:eventLabel",
"operator": "EXACT",
"expressions": ["bw"]
}
}
]
}]
}
}]
}
}
},{
"dynamicSegment":
{
"name": "version_color",
"userSegment":
{
"segmentFilters": [
{
"simpleSegment":
{
"orFiltersForSegment": [
{
"segmentFilterClauses": [
{
"dimensionFilter":
{
"dimensionName": "ga:eventAction",
"operator": "EXACT",
"expressions": ["set-visual-code"]
}
},{
"dimensionFilter":
{
"dimensionName": "ga:eventLabel",
"operator": "EXACT",
"expressions": ["color"]
}
}
]
}]
}
}]
}
}
}]
}
]
}
}, function(err, response) {
if (err) {
console.log('API Error: '+ err);
return;
}
var rows = response.reports[0].data.rows;
for (var i = 0; i < rows.length; i++) {
console.log(rows[i].dimensions);
console.log(rows[i].metrics);
}
});
Which results in following output:
I20160719-14:09:10.405(2)? [ '/', 'version_bw' ]
I20160719-14:09:10.406(2)? [ { values: [ '373', '174.11977715877438' ] } ]
I20160719-14:09:10.406(2)? [ '/', 'version_color' ]
I20160719-14:09:10.407(2)? [ { values: [ '373', '174.11977715877438' ] } ]
I20160719-14:09:10.407(2)? [ '/portfolio', 'version_bw' ]
I20160719-14:09:10.407(2)? [ { values: [ '468', '126.2876404494382' ] } ]
I20160719-14:09:10.407(2)? [ '/portfolio', 'version_color' ]
I20160719-14:09:10.408(2)? [ { values: [ '468', '126.2876404494382' ] } ]
I20160719-14:09:10.408(2)? [ '/portfolio/', 'version_bw' ]
I20160719-14:09:10.409(2)? [ { values: [ '22', '229.54545454545453' ] } ]
I20160719-14:09:10.410(2)? [ '/portfolio/', 'version_color' ]
I20160719-14:09:10.410(2)? [ { values: [ '22', '229.54545454545453' ] } ]
I20160719-14:09:10.410(2)? [ '/portfolio/graphics', 'version_bw' ]
I20160719-14:09:10.410(2)? [ { values: [ '84', '60.073170731707314' ] } ]
I20160719-14:09:10.410(2)? [ '/portfolio/graphics', 'version_color' ]
I20160719-14:09:10.410(2)? [ { values: [ '84', '60.073170731707314' ] } ]
I20160719-14:09:10.411(2)? [ '/portfolio/graphics/some-graphic', 'version_bw' ]
I20160719-14:09:10.411(2)? [ { values: [ '134', '42.02290076335878' ] } ]
I20160719-14:09:10.411(2)? [ '/portfolio/graphics/some-graphic', 'version_color' ]
I20160719-14:09:10.411(2)? [ { values: [ '134', '42.02290076335878' ] } ]

How to get the Bounding box values from a GeoJSON file?

I have of GeoJSON file. But problem is, I do not know the projection of this file. Now, I want to overlay on my base map(OSM). How should I do it? This GeoJSON file contains features around Stockholm area. How can i set the bounding box for this? I tried with couple of openlayers example from hosting sites. But, those do not work for me. I got one example which is very similar with what i want, from (http://dev.openlayers.org/releases/OpenLayers-2.11/examples/all-overlays.html). I checked the source code he used bounding box. But problem is, if I remove bounding box this example does not work(I tied with given data with this). I do not know, is there anything wrong with my json file? Bellow, I have given some sample data. Is the format OK? As I told, I tried with different way. Some time i read the json file just fine from script. But, It can not show the featured overlay ed map. Bellow, I gave sample code, that shows how I tried.
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "FNODE_": 387603, "TNODE_": 387603, "LPOLY_": 0, "RPOLY_": 0, "LENGTH": 89.206900, "ROADS_SWER": 519006, "ROADS_SW_1": 519006, "REF": "", "TYPE": "residential", "ONEWAY": 8224, "BRIDGE": 8224, "MAXSPEED": 0, "LENGTH_M": 89.500000, "ID": 0, "dist_fnode": 13655.200000, "dist_tnode": 13655.200000 }, "geometry": { "type": "LineString", "coordinates": [ [ 676868.875000, 6569872.000000 ], [ 676882.437500, 6569861.000000 ], [ 676894.062500, 6569851.500000 ], [ 676894.500000, 6569844.500000 ], [ 676891.812500, 6569840.500000 ], [ 676887.312500, 6569841.000000 ], [ 676882.187500, 6569843.000000 ], [ 676875.250000, 6569850.000000 ], [ 676868.125000, 6569858.500000 ], [ 676866.500000, 6569867.000000 ], [ 676868.875000, 6569872.000000 ] ] } }
,
{ "type": "Feature", "properties": { "FNODE_": 387723, "TNODE_": 387724, "LPOLY_": 0, "RPOLY_": 0, "LENGTH": 9.581310, "ROADS_SWER": 519163, "ROADS_SW_1": 519163, "REF": "", "TYPE": "service", "ONEWAY": 8224, "BRIDGE": 8224, "MAXSPEED": 0, "LENGTH_M": 9.500000, "ID": 1, "dist_fnode": 13705.100000, "dist_tnode": 13695.600000 }, "geometry": { "type": "LineString", "coordinates": [ [ 677125.375000, 6569906.500000 ], [ 677133.437500, 6569911.500000 ] ] } }
,
..
..
]
}
Code Sample----
//OSM Layer-----------------------------------------------
var layer = new OpenLayers.Layer.OSM( "Simple OSM Map");
//GeoJSON Layer-------------------------------------------
var vector_format = new OpenLayers.Format.GeoJSON({});
var vector_protocol = new OpenLayers.Protocol.HTTP({
url: 'ml/roads.json',
format: vector_format
});
var vector_strategies = [new OpenLayers.Strategy.Fixed()];
var vector_layer = new OpenLayers.Layer.Vector('More Advanced Vector Layer',{
protocol: vector_protocol,
strategies: vector_strategies,
isBaseLayer: false
});
var options = {
layers: [layer, vector_layer]
};
var map = new OpenLayers.Map("map", options);
//Projection-----------------------------------------------
map.setCenter(
new OpenLayers.LonLat(18.068611, 59.329444).transform(
new OpenLayers.Projection("EPSG:4326"),
map.getProjectionObject()
), 10
);
Thanks, in advance.
vector_layer.events.on({
loadend: function() {
map.zoomToExtent(vector_layer.getDataExtent());
}
});
I still think you should set your projection in the map constructor…

Resources