React
by benjrei
HTML
<div id="app"></div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.done {
color: rgba(0, 0, 0, 0.3);
text-decoration: line-through;
}
input {
margin-right: 5px;
}
React
class TodoApp extends React.Component {
constructor(props) {
super(props)
this.state = {
wkt: 'LINESTRING Z (61.70980420096773 5.919403530383208 33.7, 61.70974267535814 5.919402727673847 33.7, 61.70965584350121 5.919388478519132 33.7, 61.709571309848435 5.919354502739293 33.7, 61.709438974047984 5.919268042531684 33.8, 61.7093211101757 5.919141106277405 33.8, 61.70920669343651 5.918984581262524 34.1, 61.70909598635624 5.918788867414984 34.3, 61.709001583084586 5.918620696819111 34.6, 61.7088954324985 5.918425538795389 34.6)'
}
}
getGeometryType(word){
switch(word) {
case 'LINESTRING':
return 'LineString';
case 'POINT':
return 'Point';
default:
return 'Point'
}
}
wktToGeometry(wkt){
const geoTypeFromWkt = wkt.replace(/ .*/,'');
const type = this.getGeometryType(geoTypeFromWkt);
const regEx = /\(.*?\)/g;
const matches = wkt.match(regEx);
let coordinates = [];
matches.forEach((match)=>{
match = match.replace(/[()]/g, '');
const latLng = match.split(', ');
if(latLng.length === 1){
const arr = latLng[0].split(' ');
return coordinates = [parseFloat(arr[1]).toFixed(6), parseFloat(arr[0]).toFixed(6)];
}else if(latLng.length > 1){
let latLngTemp = [];
latLng.forEach((item)=>{
const arr = item.split(' ');
const element = [parseFloat(arr[1]).toFixed(6), parseFloat(arr[0]).toFixed(6)];
latLngTemp.push(element)
});
coordinates.push(...latLngTemp)
}
});
return {
geometry: {
type,
coordinates
}
}
};
render() {
console.log(this.wktToGeometry(this.state.wkt))
return (
<div>
<h2>Todos:</h2>
</div>
)
}
}
ReactDOM.render(<TodoApp />, document.querySelector("#app"))