mirror of
https://github.com/felt/tippecanoe.git
synced 2026-10-02 08:25:40 +02:00
Move CSV code into its own file
This commit is contained in:
@@ -56,7 +56,7 @@ tippecanoe-enumerate: enumerate.o
|
|||||||
tippecanoe-decode: decode.o projection.o mvt.o write_json.o text.o
|
tippecanoe-decode: decode.o projection.o mvt.o write_json.o text.o
|
||||||
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3
|
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3
|
||||||
|
|
||||||
tile-join: tile-join.o projection.o pool.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpull/jsonpull.o text.o evaluator.o
|
tile-join: tile-join.o projection.o pool.o mbtiles.o mvt.o memfile.o dirtiles.o jsonpull/jsonpull.o text.o evaluator.o csv.o
|
||||||
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
|
$(CXX) $(PG) $(LIBS) $(FINAL_FLAGS) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) -lm -lz -lsqlite3 -lpthread
|
||||||
|
|
||||||
tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o
|
tippecanoe-json-tool: jsontool.o jsonpull/jsonpull.o
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
#include "csv.hpp"
|
||||||
|
|
||||||
|
#define MAXLINE 10000 /* XXX */
|
||||||
|
|
||||||
|
std::vector<std::string> csv_split(char *s) {
|
||||||
|
std::vector<std::string> ret;
|
||||||
|
|
||||||
|
while (*s && *s != '\n' && *s != '\r') {
|
||||||
|
char *start = s;
|
||||||
|
int within = 0;
|
||||||
|
|
||||||
|
for (; *s && *s != '\n' && *s != '\r'; s++) {
|
||||||
|
if (*s == '"') {
|
||||||
|
within = !within;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*s == ',' && !within) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string v = std::string(start, s - start);
|
||||||
|
ret.push_back(v);
|
||||||
|
|
||||||
|
if (*s == ',') {
|
||||||
|
s++;
|
||||||
|
|
||||||
|
while (*s && isspace(*s)) {
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string csv_dequote(std::string s) {
|
||||||
|
std::string out;
|
||||||
|
for (size_t i = 0; i < s.size(); i++) {
|
||||||
|
if (s[i] == '"') {
|
||||||
|
if (i + 1 < s.size() && s[i + 1] == '"') {
|
||||||
|
out.push_back('"');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
out.push_back(s[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void readcsv(char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping) {
|
||||||
|
FILE *f = fopen(fn, "r");
|
||||||
|
if (f == NULL) {
|
||||||
|
perror(fn);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
char s[MAXLINE];
|
||||||
|
if (fgets(s, MAXLINE, f)) {
|
||||||
|
header = csv_split(s);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < header.size(); i++) {
|
||||||
|
header[i] = csv_dequote(header[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (fgets(s, MAXLINE, f)) {
|
||||||
|
std::vector<std::string> line = csv_split(s);
|
||||||
|
if (line.size() > 0) {
|
||||||
|
line[0] = csv_dequote(line[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < line.size() && i < header.size(); i++) {
|
||||||
|
// printf("putting %s\n", line[0].c_str());
|
||||||
|
mapping.insert(std::pair<std::string, std::vector<std::string>>(line[0], line));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fclose(f) != 0) {
|
||||||
|
perror("fclose");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef CSV_HPP
|
||||||
|
#define CSV_HPP
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
std::vector<std::string> csv_split(char *s);
|
||||||
|
std::string csv_dequote(std::string s);
|
||||||
|
void readcsv(char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping);
|
||||||
|
|
||||||
|
#endif
|
||||||
+25
-1
@@ -13,6 +13,10 @@ int fail = EXIT_SUCCESS;
|
|||||||
bool wrap = false;
|
bool wrap = false;
|
||||||
const char *extract = NULL;
|
const char *extract = NULL;
|
||||||
|
|
||||||
|
FILE *csvfile = NULL;
|
||||||
|
std::vector<std::string> cols;
|
||||||
|
std::vector<std::string> fields;
|
||||||
|
|
||||||
std::string buffered;
|
std::string buffered;
|
||||||
int buffered_type = -1;
|
int buffered_type = -1;
|
||||||
// 0: nothing yet
|
// 0: nothing yet
|
||||||
@@ -247,11 +251,12 @@ void process(FILE *fp, const char *fname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
const char *csv = NULL;
|
||||||
|
|
||||||
struct option long_options[] = {
|
struct option long_options[] = {
|
||||||
{"wrap", no_argument, 0, 'w'},
|
{"wrap", no_argument, 0, 'w'},
|
||||||
{"extract", required_argument, 0, 'e'},
|
{"extract", required_argument, 0, 'e'},
|
||||||
{"csv", required_argument, 0, 'c'},
|
{"csv", required_argument, 0, 'c'},
|
||||||
{"join", required_argument, 0, 'j'},
|
|
||||||
|
|
||||||
{0, 0, 0, 0},
|
{0, 0, 0, 0},
|
||||||
};
|
};
|
||||||
@@ -280,6 +285,10 @@ int main(int argc, char **argv) {
|
|||||||
extract = optarg;
|
extract = optarg;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'c':
|
||||||
|
csv = optarg;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Unexpected option -%c\n", i);
|
fprintf(stderr, "Unexpected option -%c\n", i);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -291,6 +300,14 @@ int main(int argc, char **argv) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (csv != NULL) {
|
||||||
|
csvfile = fopen(csv, "r");
|
||||||
|
if (csvfile == NULL) {
|
||||||
|
perror(csv);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (optind >= argc) {
|
if (optind >= argc) {
|
||||||
process(stdin, "standard input");
|
process(stdin, "standard input");
|
||||||
} else {
|
} else {
|
||||||
@@ -312,5 +329,12 @@ int main(int argc, char **argv) {
|
|||||||
printf("]}\n");
|
printf("]}\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (csvfile != NULL) {
|
||||||
|
if (fclose(csvfile) != 0) {
|
||||||
|
perror("close");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return fail;
|
return fail;
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-84
@@ -23,6 +23,7 @@
|
|||||||
#include "geometry.hpp"
|
#include "geometry.hpp"
|
||||||
#include "dirtiles.hpp"
|
#include "dirtiles.hpp"
|
||||||
#include "evaluator.hpp"
|
#include "evaluator.hpp"
|
||||||
|
#include "csv.hpp"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -30,8 +31,6 @@
|
|||||||
#include "jsonpull/jsonpull.h"
|
#include "jsonpull/jsonpull.h"
|
||||||
#include "milo/dtoa_milo.h"
|
#include "milo/dtoa_milo.h"
|
||||||
|
|
||||||
std::string dequote(std::string s);
|
|
||||||
|
|
||||||
int pk = false;
|
int pk = false;
|
||||||
int pC = false;
|
int pC = false;
|
||||||
int pg = false;
|
int pg = false;
|
||||||
@@ -207,7 +206,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::map<std::st
|
|||||||
|
|
||||||
if (joinval.size() > 0) {
|
if (joinval.size() > 0) {
|
||||||
if (joinval[0] == '"') {
|
if (joinval[0] == '"') {
|
||||||
joinval = dequote(joinval);
|
joinval = csv_dequote(joinval);
|
||||||
} else if ((joinval[0] >= '0' && joinval[0] <= '9') || joinval[0] == '-') {
|
} else if ((joinval[0] >= '0' && joinval[0] <= '9') || joinval[0] == '-') {
|
||||||
attr_type = mvt_double;
|
attr_type = mvt_double;
|
||||||
}
|
}
|
||||||
@@ -957,87 +956,6 @@ void usage(char **argv) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define MAXLINE 10000 /* XXX */
|
|
||||||
|
|
||||||
std::vector<std::string> split(char *s) {
|
|
||||||
std::vector<std::string> ret;
|
|
||||||
|
|
||||||
while (*s && *s != '\n' && *s != '\r') {
|
|
||||||
char *start = s;
|
|
||||||
int within = 0;
|
|
||||||
|
|
||||||
for (; *s && *s != '\n' && *s != '\r'; s++) {
|
|
||||||
if (*s == '"') {
|
|
||||||
within = !within;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*s == ',' && !within) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string v = std::string(start, s - start);
|
|
||||||
ret.push_back(v);
|
|
||||||
|
|
||||||
if (*s == ',') {
|
|
||||||
s++;
|
|
||||||
|
|
||||||
while (*s && isspace(*s)) {
|
|
||||||
s++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string dequote(std::string s) {
|
|
||||||
std::string out;
|
|
||||||
for (size_t i = 0; i < s.size(); i++) {
|
|
||||||
if (s[i] == '"') {
|
|
||||||
if (i + 1 < s.size() && s[i + 1] == '"') {
|
|
||||||
out.push_back('"');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
out.push_back(s[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
void readcsv(char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping) {
|
|
||||||
FILE *f = fopen(fn, "r");
|
|
||||||
if (f == NULL) {
|
|
||||||
perror(fn);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
char s[MAXLINE];
|
|
||||||
if (fgets(s, MAXLINE, f)) {
|
|
||||||
header = split(s);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < header.size(); i++) {
|
|
||||||
header[i] = dequote(header[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (fgets(s, MAXLINE, f)) {
|
|
||||||
std::vector<std::string> line = split(s);
|
|
||||||
if (line.size() > 0) {
|
|
||||||
line[0] = dequote(line[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < line.size() && i < header.size(); i++) {
|
|
||||||
// printf("putting %s\n", line[0].c_str());
|
|
||||||
mapping.insert(std::pair<std::string, std::vector<std::string>>(line[0], line));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fclose(f) != 0) {
|
|
||||||
perror("fclose");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
char *out_mbtiles = NULL;
|
char *out_mbtiles = NULL;
|
||||||
char *out_dir = NULL;
|
char *out_dir = NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user