How do you convert TopoJSON linestring to polygon? - topojson

I have a TopoJSON file containing the boundaries of various districts in Uttar Pradesh, India. When you load the data on a map, you see only the outlines of the districts; the districts themselves are not filled.
I believe the problem is that each district is of type GeometryCollection that has its own geometries made up of a series of LineStrings.
Instead, I want each district to be of type Polygon that just has arcs.
For example, the first object is:
{
"type": "GeometryCollection",
"geometries": [{
"type": "GeometryCollection",
"properties": {
"district_number": 1,
"district_name": "Ghaziabad"
},
"geometries": [{
"type": "LineString",
"arcs": [0]
}, {
"type": "LineString",
"arcs": [1]
}, {
"type": "LineString",
"arcs": [2]
}, {
"type": "LineString",
"arcs": [3]
}, {
"type": "LineString",
"arcs": [4]
}, {
"type": "LineString",
"arcs": [5]
}]
}
I think I want to convert it, and every other object, to:
{
"type": "Polygon",
"properties": {
"district_number": 1,
"district_name": "Ghaziabad"
},
"arcs": [[0,1,2,3,4,5]]
}
I could fix it manually, but that seems insane. Is there a better way?
Update
So I figured out how to convert the object into the result I thought I wanted, but I got some very wacky polygons. Here is my (very clunky) code. Thanks to Saeed Adel Mehraban for some guidance with this.
d3.json('map.topojson',function(error,data){ // get my json that needs to be converted
var arr = data.objects.collection.geometries; // this is the relevant array
var newArr = []; // in order to map each object, i need to put each one into a new array as a single-item array
arr.forEach(function(d,i){
var curr = [d];
newArr.push(curr);
})
newArr.forEach(function(e,i){ // now that i have my new array, i want to convert each object that contains a LineString into a Polygon
var output = e.map(function(d){
var arcsArr = []; // an empty array to push each single value of the LineString arcs into
return {
"type": "Polygon", // change the type to polygon
"properties": d.properties, // keep the properties
"arcs": d.geometries.map(function(g) { // a single key-value pair for arcs, made up of the individual arcs from the LineString
arcsArr.push(g.arcs[0]);
return [arcsArr]; // the array of arcs must be in another array
})
};
});
var output = output[0]; // get only the first item in the output array, which is the object i have modified
output.arcs = output.arcs[0]; // and change the arcs so we're only taking the first array (as we've duplicated the arrays)
$('body').append(JSON.stringify(output)+','); // append the whole thing to the body so I can copy it and paste it into the appropriate part of the JSON
});
});
This "worked" in the sense that my LineStrings were indeed converted to Polygons, retaining the original border. But the polygons themselves are a nightmare, with straight lines crisscrossing the map at all kinds of angles.
Is there something like a command line tool that can convert boundaries made of LineStrings into Polygons?

I believe I ran into the same problem being described.
This is Zambia drawn as a svg polyline foreach arc (red being the first arc listed, and magenta being the last):
However when attempting to create a polygon by concatenating the arcs:
What happened was the arcs for every object were listed clockwise, but the points in every individual arc were listed counterclockwise. Without seeing the topojson that OP is using I cannot 100% confirm this, but I suspect that this was the case.
I solved this by reversing the points in an arc before pushing them to the array of points to draw the polygon and now all is well:

Maybe a map function like below? (I write that with simplistic assumption about data schema. I can't guarantee that it works for complex linestrings since I'm not familiar with topojson format. But it works with your provided data)
var foo = [
{
"type": "GeometryCollection",
"geometries": [{
"type": "GeometryCollection",
"properties": {
"district_number": 1,
"district_name": "Ghaziabad"
},
"geometries": [{
"type": "LineString",
"arcs": [0]
}, {
"type": "LineString",
"arcs": [1]
}, {
"type": "LineString",
"arcs": [2]
}, {
"type": "LineString",
"arcs": [3]
}, {
"type": "LineString",
"arcs": [4]
}, {
"type": "LineString",
"arcs": [5]
}]
}]
}
];
var bar = foo.map(function(d) {
return {
"type": "Polygon",
"properties": d.geometries[0].properties,
"arc": d.geometries.map(function(g1) {
return g1.geometries.map(function(g) {
return g.arcs[0];
});
})
};
});
console.log(bar);

Related

amcharts 4 category X axis labels not aligned with graphed data

Using rotated labels on category X axis. (Real graph uses long names but results are the same.)
A: Alignment of axis labels and their data points is fine without label rotation.
B: Labels are not aligned when rotated.
The only difference between A and B graph coding is the addition of "labels":{"rotation":90} in the xAxes renderer.
How can alignment be fixed?
var chart =am4core.createFromConfig({
"colors": { "list": ["#DB3", "#000", "#888"] },
"data": [$data],
"legend": { "markers":{"width":20, "height":10},
"position":"top",
"useDefaultMarker":true
},
"series": [
{ "dataFields": { "categoryX":"X", "valueY":"Y1" },
"name": "[font-size:16px;font-weight:600;]{$n1}[/]",
"strokeWidth": 2,
"type": "LineSeries"
},
{ "dataFields": { "categoryX":"X", "valueY":"Y2" },
"name": "[font-size:16px;font-weight:600;]{$n2}[/]",
"strokeWidth": 2,
"type": "LineSeries"
},
{ "dataFields": { "categoryX":"X", "valueY":"Y3" },
"name": "[font-size:16px;font-weight:600;]others[/]",
"strokeWidth": 2,
"type": "LineSeries"
}
],
"xAxes": [
{ "dataFields": { "category": "X" },
"renderer": { "minGridDistance":11, "labels":{"rotation":90}},
"type": "CategoryAxis"
}
],
"yAxes": [
{ "renderer": {"minGridDistance":15},
"title": {"text": "[font-size:16px;font-weight:600;]Values[/]"},
"type": "ValueAxis"
}]
}, "line", am4charts.XYChart);
LABELS renderer needed "verticalCenter":"middle" with rotation.
I don't know how many other things I tried!

Valid Topojson and https://mapshaper.org/

All 3 cases validate in jsonlint, but only the first one is displayed in mapshaper.
I do not understand why case 2 and 3 are not displayed .
So how can I set up this correct using '-' in the arcs ref for a property ?
Displays OK in https://mapshaper.org/
{"type":"Topology", "crs":{"type":"name","properties":{"name":"EPSG:25832"}},"objects":{"collection": { "type": "GeometryCollection", "geometries":[
{ "type": "MultiPolygon", "arcs": [[[0,1]]],"properties":{"id":9005309}}]}},"arcs":
[
[[565546,7786890.97],[565545.69,7786859.81]],
[[565545.69,7786859.81],[565545.06,7786876.95],[565546,7786890.97]]]
}
Does not displays in https://mapshaper.org/
{"type":"Topology", "crs":{"type":"name","properties":{"name":"EPSG:25832"}},"objects":{"collection": { "type": "GeometryCollection", "geometries":[
{ "type": "MultiPolygon", "arcs": [[[-0,-1]]],"properties":{"id":9005309}}]}},"arcs":
[
[[565545.69,7786859.81],[565546,7786890.97]],
[[565546,7786890.97],[565545.06,7786876.95],[565545.69,7786859.81]]
]
}
Does not displays in https://mapshaper.org/
{"type":"Topology", "crs":{"type":"name","properties":{"name":"EPSG:25832"}},"objects":{"collection": { "type": "GeometryCollection", "geometries":[
{ "type": "MultiPolygon", "arcs": [[[-0,1]]],"properties":{"id":9005309}}]}},"arcs":
[
[[565545.69,7786859.81],[565546,7786890.97]],
[[565545.69,7786859.81],[565545.06,7786876.95],[565546,7786890.97]]]
}
You have the wrong index for negative (/reversed) arcs:
Each arc must be referenced by numeric zero-based index into the
containing topology’s arcs array. For example, 0 refers to the first
arc, 1 refers to the second arc, and so on.
A negative arc index indicates that the arc at the ones’ complement of
the index must be reversed to reconstruct the geometry: -1 refers to
the reversed first arc, -2 refers to the reversed second arc, and so
on. In JavaScript, you can negate a negative arc index i using the
bitwise NOT operator, ~i. (docs)
Instead of -0 you should be using -1. So your 2nd topojson example should draw as follows:
{"type":"Topology",
"crs":{"type":"name","properties": {"name":"EPSG:25832"}},
"objects":{
"collection": { "type": "GeometryCollection",
"geometries":[
{ "type": "MultiPolygon",
"arcs": [[[-1,-2]]],
"properties":{"id":9005309}}]}},
"arcs":[
[[565545.69,7786859.81],[565546,7786890.97]],
[[565546,7786890.97],[565545.06,7786876.95],[565545.69,7786859.81]]
]
}

How to save geo data in elasticsearch

How do I index a document with below data in elasticsearch(geo datatype)?
<west>5.8663152683722</west>
<north>55.0583836008072</north>
<east>15.0418156516163</east>
<south>47.2701236047002</south>
I tried geo_point and its working for lon and lat's, not sure how to save this data. any help is highly appreciated.
You'll have to use the geo_shape datatype and convert your XML (I assume) semi-points into a line string or polygon before sync.
I'm gonna go with a polygon here.
Let's visualise the conventional cardinal directions:
North (+90)
|
(-180) West ——+—— East (+180)
|
South (-90)
geo_shape expects GeoJSON-like inputs so you'll need five coordinate points, the first and last of which are identical (according to the GeoJSON spec).
Therefore, borrowing from TurfJS and going from bottom left counter-clockwise,
const lowLeft = [west, south];
const topLeft = [west, north];
const topRight = [east, north];
const lowRight = [east, south];
return
[
[
lowLeft,
lowRight,
topRight,
topLeft,
lowLeft
]
]
Finally, let's create our index and plug your numbers in
PUT /example
{
"mappings": {
"properties": {
"location": {
"type": "geo_shape"
}
}
}
}
POST /example/_doc
{
"location":{
"type":"polygon",
"coordinates":[
[
[
5.8663152683722,
47.2701236047002
],
[
15.0418156516163,
47.2701236047002
],
[
15.0418156516163,
55.0583836008072
],
[
5.8663152683722,
55.0583836008072
],
[
5.8663152683722,
47.2701236047002
]
]
]
}
}
Then verify that the center of your square if indeed inside of your indexed polygon:
GET example/_search
{
"query": {
"geo_shape": {
"location": {
"shape": {
"type": "point",
"coordinates": [
10.45406545999425,
51.1642536027537
]
},
"relation": "intersects"
}
}
}
}

Remove Antarctica from this vega / vega-lite world map

I'm trying to remove Antarctica from this Vega-lite world map. Example in vega-editor here. Wondering how I can get hold of specific data w.r.t country or continent
Use filter transnform:
{
"name": "world", // <---- Your map
"url": "https://vega.github.io/editor/data/world-110m.json",
"format": {
"type": "topojson",
"feature": "countries"
},
"transform": [
{
"type": "filter",
"expr":"datum.id!=10" //<---- Antarctica has id 10
}
]
},
See on your example

Mapbox Rotating Marker by GeoJSON

I have just started using mapbox and i have some troubles with it.
I have added angle to my geojson data in order to rotate the image to a specified angle from geojson, but I'm having trouble with writing its rotate function :(
we are acquiring our data from AJAX so the function should be compatible with AJAX.
Any help would deeply appreciated.
This is my geojson data:
{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [-79.00, 40] }, "properties": { "title": "Big astronaut","id":"100", "icon": { "iconUrl": "_vehicle.png", "iconSize": [32, 32], "iconAnchor": [50, 50], "popupAnchor": [0, -55], "className": "dot", "angle":"87" } } }
Thank you

Resources