]> Pileus Git - grits/commitdiff
Add string parser to GritsPoly
authorAndy Spencer <andy753421@gmail.com>
Wed, 12 Oct 2011 08:30:35 +0000 (08:30 +0000)
committerAndy Spencer <andy753421@gmail.com>
Wed, 12 Oct 2011 08:30:35 +0000 (08:30 +0000)
src/objects/grits-poly.c
src/objects/grits-poly.h

index 1fe2266a1d6bf13ad0d4118c3035fccf67e5b110..da78fba8db901079bac0f1ace9ad69bbbb605bf5 100644 (file)
@@ -117,6 +117,47 @@ GritsPoly *grits_poly_new(gdouble (**points)[3])
        return poly;
 }
 
+GritsPoly *grits_poly_parse(gchar *str,
+               gchar *poly_sep, gchar *point_sep, gchar *coord_sep)
+{
+       /* Split and count polygons */
+       gchar **spolys = g_strsplit(str, poly_sep, -1);
+       int     npolys = g_strv_length(spolys);
+
+       GritsPoint center = {0,0,0};
+       gdouble (**polys)[3] = (gpointer)g_new0(double*, npolys+1);
+       for (int pi = 0; pi < npolys; pi++) {
+               /* Split and count coordinates */
+               gchar **scoords = g_strsplit(spolys[pi], point_sep, -1);
+               int     ncoords = g_strv_length(scoords);
+
+               /* Create binary coords */
+               gdouble (*coords)[3] = (gpointer)g_new0(gdouble, 3*(ncoords+1));
+               for (int ci = 0; ci < ncoords; ci++) {
+                       gdouble lat, lon;
+                       sscanf(scoords[ci], "%lf,%lf", &lat, &lon);
+                       if (ci == 0) {
+                               center.lat  = lat;
+                               center.lon  = lon;
+                               center.elev = 0;
+                       }
+                       lle2xyz(lat, lon, 0,
+                                       &coords[ci][0],
+                                       &coords[ci][1],
+                                       &coords[ci][2]);
+               }
+
+               /* Insert coords into poly array */
+               polys[pi] = coords;
+               g_strfreev(scoords);
+       }
+
+       /* Create GritsPoly */
+       GritsPoly *poly = grits_poly_new(polys);
+       GRITS_OBJECT(poly)->center = center;
+       return poly;
+}
+
 /* GObject code */
 G_DEFINE_TYPE(GritsPoly, grits_poly, GRITS_TYPE_OBJECT);
 static void grits_poly_init(GritsPoly *poly)
index df70b67717c02b41ee8d1ddc34f5b767c2e27dac..262935301adfb5f3390169dc42335a13d6f57104 100644 (file)
@@ -51,4 +51,7 @@ GType grits_poly_get_type(void);
 
 GritsPoly *grits_poly_new(gdouble (**points)[3]);
 
+GritsPoly *grits_poly_parse(gchar *str,
+               gchar *poly_sep, gchar *point_sep, gchar *coord_sep);
+
 #endif