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:
@@ -55,7 +55,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
|
||||||
|
|
||||||
geojson2nd: geojson2nd.o jsonpull/jsonpull.o
|
geojson2nd: geojson2nd.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
|
||||||
+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;
|
||||||
@@ -213,7 +212,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;
|
||||||
}
|
}
|
||||||
@@ -979,87 +978,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