Skip to content

Instantly share code, notes, and snippets.

View benjbaron's full-sized avatar

Benjamin Baron benjbaron

View GitHub Profile
@benjbaron
benjbaron / ConvertFromGEOStoOGR.cpp
Last active April 26, 2018 13:33
Use OGR to parse a Shapefile (here, we want to parse an OSM Shapefile to get the main roads)
OGRLineString * convertFromGEOStoOGR(LineString * ls) {
OGRLineString * lineString = (OGRLineString*) OGRGeometryFactory::createGeometry(wkbLineString);
for(int i = 0; i < ls->getNumPoints(); ++i) {
Point * pt = ls->getPointN(i);
lineString->addPoint(pt->getX(), pt->getY());
}
return lineString;
}
@benjbaron
benjbaron / CSVParser.cpp
Created July 10, 2015 08:35
Parse a CSV file with Qt with regex or by specifying the characters delimiters, escape and quote
class CSVParser
{
public:
CSVParser() {}
static int parseString(QString &buffer, QStringList &fields, QString delimChars, QString quoteChars, QString escapeChars) {
QString field; // String in which to accumulate next field
bool escaped = false; // Next char is escaped
bool quoted = false; // In quotes
QChar quoteChar = 0; // Actual quote character used to open quotes
@benjbaron
benjbaron / CSVParser.java
Created July 10, 2015 08:33
Read CSV with a header
public static ArrayList<Map<String, String>> readCSV(String filePath) throws IOException {
InputStream is = new FileInputStream(filePath);
ArrayList<Map<String, String>> al = new ArrayList<Map<String, String>>();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String[] headerLine = br.readLine().split(","); // reads the header of the file
String line = br.readLine(); // reads the first line of data
while (line != null) {
String[] values = line.split(",");
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < values.length; i++) {
@benjbaron
benjbaron / QGraphicsSceneTest.cpp
Last active April 22, 2022 03:13
Qt QGraphicsScene click, select, move, resize, delete QGraphicsItems
#include <QtGui>
#include <QGraphicsRectItem>
#include <QGraphicsView>
#include <QApplication>
#include <QGraphicsSceneMouseEvent>
class CustomItem : public QGraphicsEllipseItem
{
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event)
@benjbaron
benjbaron / 0_reuse_code.js
Last active August 29, 2015 14:07
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console