def feature_collection(
    data, lat_field="latitude", lon_field="longitude", spatial_ref=4326
):
    """
    Assemble an ArcREST featureCollection object
    spec: http://resources.arcgis.com/en/help/arcgis-rest-api/#/Feature_object/02r3000000n8000000/
    """
    features = []

    for record in data:

        new_record = {
            "attributes": {},
            "geometry": {"spatialReference": {"wkid": spatial_ref}},
        }

        new_record["attributes"] = record

        if record.get("paths"):
            # Handle polyline geometry
            new_record["geometry"]["paths"] = record.pop("paths")

        elif record.get(lat_field) and record.get(lon_field):
            # Handle point geometry
            new_record["geometry"]["x"] = record.get(lon_field)
            new_record["geometry"]["y"] = record.get(lat_field)

        else:
            #  strip geometry from records with missing/unkown geometry data
            new_record.pop("geometry")

        features.append(new_record)

    return features