sadaszewski / concaveman-cpp

C++ port of mapbox's JS concaveman, with a Python wrapper
BSD 2-Clause "Simplified" License
151 stars 40 forks source link

pandas to_numpy makes inncorrect answer #16

Closed hiromuhana closed 3 years ago

hiromuhana commented 3 years ago

When I made "pts" in python code, I used pandas dataframe and converted to ndarray. But the program returned invalid answer.

pts = df[['long', 'lat']].to_numpy()

Wheras, I converted list type first and converted np.array next, the program returned correct answer.

pts = df[['long', 'lat']].to_numpy().tolist() pts = np.array(pts)

What's happening?

sadaszewski commented 3 years ago

The first thing that comes to mind is that the data is not contiguous coming from pandas. Does pts = np.ascontiguousarray(df[['long', 'lat']].to_numpy()) work? If not, maybe you could provide the smallest standalone reproducible example code? Good luck.

hiromuhana commented 3 years ago

Thank you very much for your advice! np.ascontiguousarray(df[['long', 'lat']] worked correctly.

I'll share my code just in case.

from concaveman import concaveman2d import json import numpy as np from scipy.spatial import ConvexHull import csv import pandas as pd from shapely import geometry import sys sys.path.append('/home/jovyan/work/concaveman-cpp/src/main/python/')

df = pd.read_csv('/home/jovyan/work/concaveman-cpp/data/kokyoshisetsu.csv', encoding='sjis', usecols=['long', 'lat'], header=0)

pts = df[['long', 'lat']].to_numpy().tolist() pts = np.array(pts)

h = ConvexHull(pts) cc = concaveman2d(pts, h.vertices, 2, 0.005)

np.savetxt('/home/jovyan/work/concaveman-cpp/hoge.csv', cc, delimiter=',', header='long, lat', fmt='%.5f')

poly = geometry.Polygon([[p[0], p[1]] for p in cc]) display(poly)