ref: d6a46d18dc03f793e31b0a8336e7de4c77b3a4d1
parent: cfddbc180a20062d424149829cc64fbf858b0812
author: sirjofri <sirjofri@sirjofri.de>
date: Fri Apr 4 17:57:09 EDT 2025
adds first geojson rendering (lines only)
--- a/fns.h
+++ b/fns.h
@@ -5,7 +5,7 @@
int parsepos(char *data, int ndata, GPos *pos, int *zoom);
int handlegeojson(char *data, int ndata);
-void rendergeojson(GPos pos, int zoom);
+void rendergeojson(GBundle pos);
void cleargeojson(void);
void lockmapimage(void);
--- a/geojson.c
+++ b/geojson.c
@@ -16,6 +16,7 @@
static char *Tpolygon = "Polygon";
extern Image *mapimage;
+extern int tilesize;
static JSON *geojson = nil;
@@ -52,10 +53,35 @@
return 1;
}
-static GPos jpos;
-static int jzoom;
+static GBundle jpos;
static int renderjsontype(JSON*);
+static void
+drawline(GPos f, GPos t)
+{
+ GBundle fb, tb;
+ Point fo, to;
+
+ fb = getbundle(f, jpos.z, &fo);
+ tb = getbundle(t, jpos.z, &to);
+
+ fb.x -= jpos.x;
+ fb.y -= jpos.y;
+ tb.x -= jpos.x;
+ tb.y -= jpos.y;
+
+ fo.x = fb.x * tilesize + fb.x;
+ fo.y = fb.y * tilesize + fb.y;
+ to.x = tb.x * tilesize + tb.x;
+ to.y = tb.y * tilesize + tb.y;
+
+ debugprint("jsline: %P - %P\n", fo, to);
+
+ line(mapimage, fo, to,
+ Endsquare, Endsquare,
+ 0, display->black, ZP);
+}
+
static int
renderfeaturecollection(JSON *j)
{
@@ -92,6 +118,8 @@
JSONEl *el;
JSON *last = nil;
double x1 = 0., y1 = 0., x2 = 0., y2 = 0.;
+ GPos f, t;
+
if (!(j && j->t == JSONArray))
return 0;
@@ -108,7 +136,12 @@
continue;
}
last = el->val;
- debugprint("LINE: %f, %f - %f %f\n", x1, y1, x2, y2);
+
+ f.lon = x1;
+ f.lat = y1;
+ t.lon = x2;
+ t.lat = y2;
+ drawline(f, t);
}
if (l)
@@ -120,13 +153,13 @@
renderpolygon(JSON *j)
{
JSON *last;
- int ret;
double x1, y1, x2, y2;
+ GPos f, t;
+
if (!(j && j->t == JSONArray))
return 0;
- ret = renderlinestring(j, &last);
- if (!ret)
+ if (!renderlinestring(j, &last))
return 0;
if (!last)
@@ -133,11 +166,16 @@
return 0;
if (!jsoncoords(j->first->val, &x2, &y2))
- return ret;
+ return 0;
if (!jsoncoords(last, &x1, &y1))
- return ret;
- debugprint("LINE: %f, %f - %f, %f\n", x1, y1, x2, y2);
+ return 0;
+ f.lon = x1;
+ f.lat = y1;
+ t.lon = x2;
+ t.lat = y2;
+ drawline(f, t);
+
return 1;
}
@@ -155,7 +193,6 @@
return 0;
s = jsonstr(type);
- debugprint("parsing geo %s\n", s);
if (!s)
return 0;
if (strcmp(s, Tpoint) == 0)
@@ -178,7 +215,6 @@
return 0;
s = jsonstr(type);
- debugprint("parsing %s\n", s);
if (!s)
return 0;
if (strcmp(s, Tfeaturecollection) == 0)
@@ -189,7 +225,7 @@
}
void
-rendergeojson(GPos pos, int zoom)
+rendergeojson(GBundle pos)
{
if (!geojson)
return;
@@ -202,7 +238,6 @@
debugprint("rendering geojson\n");
jpos = pos;
- jzoom = zoom;
if (!renderjsontype(geojson)) {
jsonfree(geojson);
--- a/map.c
+++ b/map.c
@@ -216,7 +216,7 @@
imageupdated(void)
{
lockmapimage();
- rendergeojson(gpsoff(), currentloc.z);
+ rendergeojson(currentloc);
unlockmapimage();
qlock(&shouldredrawl);